ntp: refactor switching between online and offline state

Use an enum to describe connectivity of a source and merge
the NCR and NSR TakeSourceOnline/Offline() functions into
SetConnectivity() functions.
This commit is contained in:
Miroslav Lichvar 2018-05-24 13:42:52 +02:00
parent 2962fc6286
commit ce6b896948
10 changed files with 78 additions and 113 deletions

View file

@ -1105,7 +1105,7 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
msg->data.ntp_source.asymmetry = UTI_FloatHostToNetwork(data.params.asymmetry); msg->data.ntp_source.asymmetry = UTI_FloatHostToNetwork(data.params.asymmetry);
msg->data.ntp_source.offset = UTI_FloatHostToNetwork(data.params.offset); msg->data.ntp_source.offset = UTI_FloatHostToNetwork(data.params.offset);
msg->data.ntp_source.flags = htonl( msg->data.ntp_source.flags = htonl(
(data.params.online ? REQ_ADDSRC_ONLINE : 0) | (data.params.connectivity == SRC_ONLINE ? REQ_ADDSRC_ONLINE : 0) |
(data.params.auto_offline ? REQ_ADDSRC_AUTOOFFLINE : 0) | (data.params.auto_offline ? REQ_ADDSRC_AUTOOFFLINE : 0) |
(data.params.iburst ? REQ_ADDSRC_IBURST : 0) | (data.params.iburst ? REQ_ADDSRC_IBURST : 0) |
(data.params.interleaved ? REQ_ADDSRC_INTERLEAVED : 0) | (data.params.interleaved ? REQ_ADDSRC_INTERLEAVED : 0) |

View file

@ -424,7 +424,7 @@ handle_online(CMD_Request *rx_message, CMD_Reply *tx_message)
UTI_IPNetworkToHost(&rx_message->data.online.mask, &mask); UTI_IPNetworkToHost(&rx_message->data.online.mask, &mask);
UTI_IPNetworkToHost(&rx_message->data.online.address, &address); UTI_IPNetworkToHost(&rx_message->data.online.address, &address);
if (!NSR_TakeSourcesOnline(&mask, &address)) if (!NSR_SetConnectivity(&mask, &address, SRC_ONLINE))
tx_message->status = htons(STT_NOSUCHSOURCE); tx_message->status = htons(STT_NOSUCHSOURCE);
} }
@ -437,7 +437,7 @@ handle_offline(CMD_Request *rx_message, CMD_Reply *tx_message)
UTI_IPNetworkToHost(&rx_message->data.offline.mask, &mask); UTI_IPNetworkToHost(&rx_message->data.offline.mask, &mask);
UTI_IPNetworkToHost(&rx_message->data.offline.address, &address); UTI_IPNetworkToHost(&rx_message->data.offline.address, &address);
if (!NSR_TakeSourcesOffline(&mask, &address)) if (!NSR_SetConnectivity(&mask, &address, SRC_OFFLINE))
tx_message->status = htons(STT_NOSUCHSOURCE); tx_message->status = htons(STT_NOSUCHSOURCE);
} }
@ -797,7 +797,8 @@ handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_m
params.asymmetry = UTI_FloatNetworkToHost(rx_message->data.ntp_source.asymmetry); params.asymmetry = UTI_FloatNetworkToHost(rx_message->data.ntp_source.asymmetry);
params.offset = UTI_FloatNetworkToHost(rx_message->data.ntp_source.offset); params.offset = UTI_FloatNetworkToHost(rx_message->data.ntp_source.offset);
params.online = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_ONLINE ? 1 : 0; params.connectivity = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_ONLINE ?
SRC_ONLINE : SRC_OFFLINE;
params.auto_offline = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_AUTOOFFLINE ? 1 : 0; params.auto_offline = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_AUTOOFFLINE ? 1 : 0;
params.iburst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_IBURST ? 1 : 0; params.iburst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_IBURST ? 1 : 0;
params.interleaved = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_INTERLEAVED ? 1 : 0; params.interleaved = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_INTERLEAVED ? 1 : 0;

View file

@ -48,7 +48,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
src->port = SRC_DEFAULT_PORT; src->port = SRC_DEFAULT_PORT;
src->params.minpoll = SRC_DEFAULT_MINPOLL; src->params.minpoll = SRC_DEFAULT_MINPOLL;
src->params.maxpoll = SRC_DEFAULT_MAXPOLL; src->params.maxpoll = SRC_DEFAULT_MAXPOLL;
src->params.online = 1; src->params.connectivity = SRC_ONLINE;
src->params.auto_offline = 0; src->params.auto_offline = 0;
src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL; src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL;
src->params.burst = 0; src->params.burst = 0;
@ -90,7 +90,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
} else if (!strcasecmp(cmd, "iburst")) { } else if (!strcasecmp(cmd, "iburst")) {
src->params.iburst = 1; src->params.iburst = 1;
} else if (!strcasecmp(cmd, "offline")) { } else if (!strcasecmp(cmd, "offline")) {
src->params.online = 0; src->params.connectivity = SRC_OFFLINE;
} else if (!strcasecmp(cmd, "noselect")) { } else if (!strcasecmp(cmd, "noselect")) {
src->params.sel_options |= SRC_SELECT_NOSELECT; src->params.sel_options |= SRC_SELECT_NOSELECT;
} else if (!strcasecmp(cmd, "prefer")) { } else if (!strcasecmp(cmd, "prefer")) {

2
conf.c
View file

@ -1411,7 +1411,7 @@ CNF_AddInitSources(void)
ntp_addr.ip_addr = *(IPAddr *)ARR_GetElement(init_sources, i); ntp_addr.ip_addr = *(IPAddr *)ARR_GetElement(init_sources, i);
ntp_addr.port = cps_source.port; ntp_addr.port = cps_source.port;
cps_source.params.iburst = 1; cps_source.params.iburst = 1;
cps_source.params.online = 0; cps_source.params.connectivity = SRC_OFFLINE;
NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params); NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params);
} }

View file

@ -603,7 +603,7 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar
result->rx_timeout_id = 0; result->rx_timeout_id = 0;
result->tx_timeout_id = 0; result->tx_timeout_id = 0;
result->tx_suspended = 1; result->tx_suspended = 1;
result->opmode = params->online ? MD_ONLINE : MD_OFFLINE; result->opmode = params->connectivity == SRC_ONLINE ? MD_ONLINE : MD_OFFLINE;
result->local_poll = result->minpoll; result->local_poll = result->minpoll;
result->poll_score = 0.0; result->poll_score = 0.0;
zero_local_timestamp(&result->local_tx); zero_local_timestamp(&result->local_tx);
@ -1143,7 +1143,7 @@ transmit_timeout(void *arg)
/* With auto_offline take the source offline on 2nd missed reply */ /* With auto_offline take the source offline on 2nd missed reply */
if (inst->auto_offline && inst->tx_count >= 2) if (inst->auto_offline && inst->tx_count >= 2)
NCR_TakeSourceOffline(inst); NCR_SetConnectivity(inst, SRC_OFFLINE);
if (inst->opmode == MD_OFFLINE) { if (inst->opmode == MD_OFFLINE) {
return; return;
@ -2275,52 +2275,60 @@ NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double dof
/* ================================================== */ /* ================================================== */
void void
NCR_TakeSourceOnline(NCR_Instance inst) NCR_SetConnectivity(NCR_Instance inst, SRC_Connectivity connectivity)
{ {
switch (inst->opmode) { char *s;
case MD_ONLINE:
/* Nothing to do */ s = UTI_IPToString(&inst->remote_addr.ip_addr);
switch (connectivity) {
case SRC_ONLINE:
switch (inst->opmode) {
case MD_ONLINE:
/* Nothing to do */
break;
case MD_OFFLINE:
LOG(LOGS_INFO, "Source %s online", s);
inst->opmode = MD_ONLINE;
NCR_ResetInstance(inst);
start_initial_timeout(inst);
break;
case MD_BURST_WAS_ONLINE:
/* Will revert */
break;
case MD_BURST_WAS_OFFLINE:
inst->opmode = MD_BURST_WAS_ONLINE;
LOG(LOGS_INFO, "Source %s online", s);
break;
default:
assert(0);
}
break; break;
case MD_OFFLINE: case SRC_OFFLINE:
LOG(LOGS_INFO, "Source %s online", UTI_IPToString(&inst->remote_addr.ip_addr)); switch (inst->opmode) {
inst->opmode = MD_ONLINE; case MD_ONLINE:
NCR_ResetInstance(inst); LOG(LOGS_INFO, "Source %s offline", s);
start_initial_timeout(inst); take_offline(inst);
break; break;
case MD_BURST_WAS_ONLINE: case MD_OFFLINE:
/* Will revert */ break;
break; case MD_BURST_WAS_ONLINE:
case MD_BURST_WAS_OFFLINE: inst->opmode = MD_BURST_WAS_OFFLINE;
inst->opmode = MD_BURST_WAS_ONLINE; LOG(LOGS_INFO, "Source %s offline", s);
LOG(LOGS_INFO, "Source %s online", UTI_IPToString(&inst->remote_addr.ip_addr)); break;
case MD_BURST_WAS_OFFLINE:
break;
default:
assert(0);
}
break; break;
default:
assert(0);
} }
} }
/* ================================================== */ /* ================================================== */
void
NCR_TakeSourceOffline(NCR_Instance inst)
{
switch (inst->opmode) {
case MD_ONLINE:
LOG(LOGS_INFO, "Source %s offline", UTI_IPToString(&inst->remote_addr.ip_addr));
take_offline(inst);
break;
case MD_OFFLINE:
break;
case MD_BURST_WAS_ONLINE:
inst->opmode = MD_BURST_WAS_OFFLINE;
LOG(LOGS_INFO, "Source %s offline", UTI_IPToString(&inst->remote_addr.ip_addr));
break;
case MD_BURST_WAS_OFFLINE:
break;
}
}
/* ================================================== */
void void
NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll) NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll)
{ {

View file

@ -99,12 +99,9 @@ extern void NCR_ProcessTxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Addr
/* Slew receive and transmit times in instance records */ /* Slew receive and transmit times in instance records */
extern void NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double doffset); extern void NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double doffset);
/* Take a particular source online (i.e. start sampling it) */ /* Take a particular source online (i.e. start sampling it) or offline
extern void NCR_TakeSourceOnline(NCR_Instance inst); (i.e. stop sampling it) */
extern void NCR_SetConnectivity(NCR_Instance inst, SRC_Connectivity connectivity);
/* Take a particular source offline (i.e. stop sampling it, without
marking it unreachable in the source selection stuff) */
extern void NCR_TakeSourceOffline(NCR_Instance inst);
extern void NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll); extern void NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll);

View file

@ -860,48 +860,14 @@ slew_sources(struct timespec *raw,
/* ================================================== */ /* ================================================== */
int int
NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address) NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity)
{
SourceRecord *record;
unsigned int i;
int any;
NSR_ResolveSources();
any = 0;
for (i = 0; i < ARR_GetSize(records); i++) {
record = get_record(i);
if (record->remote_addr) {
if (address->family == IPADDR_UNSPEC ||
!UTI_CompareIPs(&record->remote_addr->ip_addr, address, mask)) {
any = 1;
NCR_TakeSourceOnline(record->data);
}
}
}
if (address->family == IPADDR_UNSPEC) {
struct UnresolvedSource *us;
for (us = unresolved_sources; us; us = us->next) {
if (us->replacement)
continue;
any = 1;
us->new_source.params.online = 1;
}
}
return any;
}
/* ================================================== */
int
NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address)
{ {
SourceRecord *record, *syncpeer; SourceRecord *record, *syncpeer;
unsigned int i, any; unsigned int i, any;
if (connectivity != SRC_OFFLINE)
NSR_ResolveSources();
any = 0; any = 0;
syncpeer = NULL; syncpeer = NULL;
for (i = 0; i < ARR_GetSize(records); i++) { for (i = 0; i < ARR_GetSize(records); i++) {
@ -914,15 +880,14 @@ NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address)
syncpeer = record; syncpeer = record;
continue; continue;
} }
NCR_TakeSourceOffline(record->data); NCR_SetConnectivity(record->data, connectivity);
} }
} }
} }
/* Take sync peer offline as last to avoid reference switching */ /* Set the sync peer last to avoid unnecessary reference switching */
if (syncpeer) { if (syncpeer)
NCR_TakeSourceOffline(syncpeer->data); NCR_SetConnectivity(syncpeer->data, connectivity);
}
if (address->family == IPADDR_UNSPEC) { if (address->family == IPADDR_UNSPEC) {
struct UnresolvedSource *us; struct UnresolvedSource *us;
@ -931,7 +896,7 @@ NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address)
if (us->replacement) if (us->replacement)
continue; continue;
any = 1; any = 1;
us->new_source.params.online = 0; us->new_source.params.connectivity = connectivity;
} }
} }

View file

@ -102,14 +102,9 @@ extern void NSR_Initialise(void);
extern void NSR_Finalise(void); extern void NSR_Finalise(void);
/* This routine is used to indicate that sources whose IP addresses /* This routine is used to indicate that sources whose IP addresses
match a particular subnet should be set online again. Returns a match a particular subnet should be set online or offline. It returns
flag indicating whether any hosts matched the address */ a flag indicating whether any hosts matched the address. */
extern int NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address); extern int NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity);
/* This routine is used to indicate that sources whose IP addresses
match a particular subnet should be set offline. Returns a flag
indicating whether any hosts matched the address */
extern int NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address);
extern int NSR_ModifyMinpoll(IPAddr *address, int new_minpoll); extern int NSR_ModifyMinpoll(IPAddr *address, int new_minpoll);

View file

@ -29,10 +29,15 @@
#include "sources.h" #include "sources.h"
typedef enum {
SRC_OFFLINE,
SRC_ONLINE,
} SRC_Connectivity;
typedef struct { typedef struct {
int minpoll; int minpoll;
int maxpoll; int maxpoll;
int online; SRC_Connectivity connectivity;
int auto_offline; int auto_offline;
int presend_minpoll; int presend_minpoll;
int burst; int burst;

View file

@ -254,13 +254,7 @@ NSR_GetLocalRefid(IPAddr *address)
} }
int int
NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address) NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity)
{
return 0;
}
int
NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address)
{ {
return 0; return 0;
} }