Skip to content

Commit

Permalink
Add random delay to announce timeout.
Browse files Browse the repository at this point in the history
According to 9.2.6.11 of the spec the ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES
timeout in addition to announceReceiptTimeoutInterval includes a random
number up to one announceInterval.

Add a new function for setting random timeout and use it in
port_set_announce_tmo().

Signed-off-by: Miroslav Lichvar <[email protected]>
  • Loading branch information
mlichvar authored and richardcochran committed Oct 27, 2013
1 parent ed379b4 commit e425da2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
27 changes: 25 additions & 2 deletions port.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,29 @@ int set_tmo_lin(int fd, int seconds)
return timerfd_settime(fd, 0, &tmo, NULL);
}

int set_tmo_random(int fd, int min, int span, int log_seconds)
{
uint64_t value_ns, min_ns, span_ns;
struct itimerspec tmo = {
{0, 0}, {0, 0}
};

if (log_seconds >= 0) {
min_ns = min * NS_PER_SEC << log_seconds;
span_ns = span * NS_PER_SEC << log_seconds;
} else {
min_ns = min * NS_PER_SEC >> -log_seconds;
span_ns = span * NS_PER_SEC >> -log_seconds;
}

value_ns = min_ns + (span_ns * (random() % (1 << 15) + 1) >> 15);

tmo.it_value.tv_sec = value_ns / NS_PER_SEC;
tmo.it_value.tv_nsec = value_ns % NS_PER_SEC;

return timerfd_settime(fd, 0, &tmo, NULL);
}

static void fc_clear(struct foreign_clock *fc)
{
struct ptp_message *m;
Expand Down Expand Up @@ -786,8 +809,8 @@ static void port_nrate_initialize(struct port *p)

static int port_set_announce_tmo(struct port *p)
{
return set_tmo_log(p->fda.fd[FD_ANNOUNCE_TIMER],
p->announceReceiptTimeout, p->logAnnounceInterval);
return set_tmo_random(p->fda.fd[FD_ANNOUNCE_TIMER],
p->announceReceiptTimeout, 1, p->logAnnounceInterval);
}

static int port_set_delay_tmo(struct port *p)
Expand Down
15 changes: 15 additions & 0 deletions port.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ enum port_state port_state(struct port *port);
*/
int set_tmo_log(int fd, unsigned int scale, int log_seconds);

/**
* Utility function for setting a file descriptor timer.
*
* This function sets the timer 'fd' to a random value between M * 2^N and
* (M + S) * 2^N, where M is the value of the 'min' parameter, S is the value
* of the 'span' parameter, and N in the value of the 'log_seconds' parameter.
*
* @param fd A file descriptor previously opened with timerfd_create(2).
* @param min The minimum value for the timer.
* @param span The span value for the timer. Must be a positive value.
* @param log_seconds The exponential factor for the timer.
* @return Zero on success, non-zero otherwise.
*/
int set_tmo_random(int fd, int min, int span, int log_seconds);

/**
* Utility function for setting or resetting a file descriptor timer.
*
Expand Down

0 comments on commit e425da2

Please sign in to comment.