ntp: don't replace source instance when changing address

Add new functions to change source's reference ID/address and reset the
instance. Use that instead of destroying and creating a new instance
when the NTP address is changed.
This commit is contained in:
Miroslav Lichvar 2014-12-01 18:01:11 +01:00
parent 308bcae257
commit 42dd5caa1b
5 changed files with 53 additions and 28 deletions

View file

@ -541,8 +541,6 @@ NCR_ResetInstance(NCR_Instance instance)
void void
NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr) NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr)
{ {
SRC_SelectOption sel_option;
inst->remote_addr = *remote_addr; inst->remote_addr = *remote_addr;
inst->tx_count = 0; inst->tx_count = 0;
inst->presend_done = 0; inst->presend_done = 0;
@ -552,11 +550,10 @@ NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr)
else else
inst->local_addr.sock_fd = NIO_GetServerSocket(remote_addr); inst->local_addr.sock_fd = NIO_GetServerSocket(remote_addr);
/* Replace sources and sourcestats instances */ /* Update the reference ID and reset the source/sourcestats instances */
sel_option = SRC_GetSelectOption(inst->source); SRC_SetRefid(inst->source, UTI_IPToRefid(&remote_addr->ip_addr),
SRC_DestroyInstance(inst->source); &inst->remote_addr.ip_addr);
inst->source = SRC_CreateNewInstance(UTI_IPToRefid(&remote_addr->ip_addr), SRC_NTP, SRC_ResetInstance(inst->source);
sel_option, &inst->remote_addr.ip_addr);
} }
/* ================================================== */ /* ================================================== */

View file

@ -231,20 +231,14 @@ SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, SRC_SelectOpt
} }
sources[n_sources] = result; sources[n_sources] = result;
result->index = n_sources; result->index = n_sources;
result->leap_status = LEAP_Normal;
result->ref_id = ref_id;
result->ip_addr = addr;
result->active = 0;
result->updates = 0;
result->reachability = 0;
result->reachability_size = 0;
result->distant = 0;
result->status = SRC_BAD_STATS;
result->type = type; result->type = type;
result->sel_score = 1.0;
result->sel_option = sel_option; result->sel_option = sel_option;
SRC_SetRefid(result, ref_id, addr);
SRC_ResetInstance(result);
n_sources++; n_sources++;
return result; return result;
@ -278,6 +272,33 @@ void SRC_DestroyInstance(SRC_Instance instance)
--selected_source_index; --selected_source_index;
} }
/* ================================================== */
void
SRC_ResetInstance(SRC_Instance instance)
{
instance->leap_status = LEAP_Normal;
instance->active = 0;
instance->updates = 0;
instance->reachability = 0;
instance->reachability_size = 0;
instance->distant = 0;
instance->status = SRC_BAD_STATS;
instance->sel_score = 1.0;
SST_ResetInstance(instance->stats);
}
/* ================================================== */
void
SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPAddr *addr)
{
instance->ref_id = ref_id;
instance->ip_addr = addr;
SST_SetRefid(instance->stats, ref_id, addr);
}
/* ================================================== */ /* ================================================== */
/* Function to get the range of frequencies, relative to the given /* Function to get the range of frequencies, relative to the given
source, that we believe the local clock lies within. The return source, that we believe the local clock lies within. The return
@ -1318,10 +1339,3 @@ SRC_Samples(SRC_Instance inst)
} }
/* ================================================== */ /* ================================================== */
SRC_SelectOption SRC_GetSelectOption(SRC_Instance inst)
{
return inst->sel_option;
}
/* ================================================== */

View file

@ -74,6 +74,11 @@ extern SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, SRC_Se
extern void SRC_DestroyInstance(SRC_Instance instance); extern void SRC_DestroyInstance(SRC_Instance instance);
/* Function to reset a source */
extern void SRC_ResetInstance(SRC_Instance instance);
/* Function to change the sources's reference ID and IP address */
extern void SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPAddr *addr);
/* Function to get the range of frequencies, relative to the given /* Function to get the range of frequencies, relative to the given
source, that we believe the local clock lies within. The return source, that we believe the local clock lies within. The return
@ -174,7 +179,5 @@ extern SRC_Type SRC_GetType(int index);
extern int SRC_Samples(SRC_Instance inst); extern int SRC_Samples(SRC_Instance inst);
extern SRC_SelectOption SRC_GetSelectOption(SRC_Instance inst);
#endif /* GOT_SOURCES_H */ #endif /* GOT_SOURCES_H */

View file

@ -182,9 +182,8 @@ SST_CreateInstance(uint32_t refid, IPAddr *addr)
{ {
SST_Stats inst; SST_Stats inst;
inst = MallocNew(struct SST_Stats_Record); inst = MallocNew(struct SST_Stats_Record);
inst->refid = refid;
inst->ip_addr = addr;
SST_SetRefid(inst, refid, addr);
SST_ResetInstance(inst); SST_ResetInstance(inst);
return inst; return inst;
@ -220,6 +219,15 @@ SST_ResetInstance(SST_Stats inst)
inst->nruns = 0; inst->nruns = 0;
} }
/* ================================================== */
void
SST_SetRefid(SST_Stats inst, uint32_t refid, IPAddr *addr)
{
inst->refid = refid;
inst->ip_addr = addr;
}
/* ================================================== */ /* ================================================== */
/* This function is called to prune the register down when it is full. /* This function is called to prune the register down when it is full.
For now, just discard the oldest sample. */ For now, just discard the oldest sample. */

View file

@ -46,6 +46,9 @@ extern void SST_DeleteInstance(SST_Stats inst);
/* This function resets an instance */ /* This function resets an instance */
extern void SST_ResetInstance(SST_Stats inst); extern void SST_ResetInstance(SST_Stats inst);
/* This function changes the reference ID and IP address */
extern void SST_SetRefid(SST_Stats inst, uint32_t refid, IPAddr *addr);
/* This function accumulates a single sample into the statistics handler /* This function accumulates a single sample into the statistics handler
sample_time is the epoch at which the sample is to be considered to sample_time is the epoch at which the sample is to be considered to