Miroslav Lichvar
2013-10-14 16:09:40 UTC
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 <***@redhat.com>
---
port.c | 27 +++++++++++++++++++++++++--
port.h | 15 +++++++++++++++
2 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/port.c b/port.c
index d98bb80..32dc22f 100644
--- a/port.c
+++ b/port.c
@@ -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 + random() * (span_ns / RAND_MAX + 1) % (span_ns + 1);
+
+ 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;
@@ -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)
diff --git a/port.h b/port.h
index 5fef028..e781f24 100644
--- a/port.h
+++ b/port.h
@@ -174,6 +174,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.
*
* This function sets the timer 'fd' to the value of the 'seconds' parameter.
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 <***@redhat.com>
---
port.c | 27 +++++++++++++++++++++++++--
port.h | 15 +++++++++++++++
2 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/port.c b/port.c
index d98bb80..32dc22f 100644
--- a/port.c
+++ b/port.c
@@ -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 + random() * (span_ns / RAND_MAX + 1) % (span_ns + 1);
+
+ 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;
@@ -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)
diff --git a/port.h b/port.h
index 5fef028..e781f24 100644
--- a/port.h
+++ b/port.h
@@ -174,6 +174,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.
*
* This function sets the timer 'fd' to the value of the 'seconds' parameter.
--
1.8.3.1
1.8.3.1