From 15443cc93f998354413ad7c1756d7607cfc98d61 Mon Sep 17 00:00:00 2001 From: Damian Krolik Date: Thu, 28 Jul 2022 14:29:58 +0200 Subject: [PATCH] [mrp] Align ReliableMessageMgr::GetBackoff() naming with spec Make variables and comments in the function more aligned with the current spec version so that it is easier to compare the code with the spec. Signed-off-by: Damian Krolik --- src/messaging/ReliableMessageMgr.cpp | 55 +++++++++++++++------------- src/messaging/ReliableMessageMgr.h | 4 +- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/messaging/ReliableMessageMgr.cpp b/src/messaging/ReliableMessageMgr.cpp index 2acb7827cff48e..f764d62c8cde30 100644 --- a/src/messaging/ReliableMessageMgr.cpp +++ b/src/messaging/ReliableMessageMgr.cpp @@ -202,40 +202,45 @@ CHIP_ERROR ReliableMessageMgr::AddToRetransTable(ReliableMessageContext * rc, Re return CHIP_NO_ERROR; } -System::Clock::Timestamp ReliableMessageMgr::GetBackoff(System::Clock::Timestamp backoffBase, uint8_t sendCount) +System::Clock::Timestamp ReliableMessageMgr::GetBackoff(System::Clock::Timestamp baseInterval, uint8_t sendCount) { - static constexpr uint32_t MRP_BACKOFF_JITTER_BASE = 1024; - static constexpr uint32_t MRP_BACKOFF_BASE_NUMERATOR = 16; - static constexpr uint32_t MRP_BACKOFF_BASE_DENOMENATOR = 10; - static constexpr uint32_t MRP_BACKOFF_THRESHOLD = 1; - - System::Clock::Timestamp backoff = backoffBase; - - // Implement `t = i⋅MRP_BACKOFF_BASE^max(0,n−MRP_BACKOFF_THRESHOLD)` from Section 4.11.2.1. Retransmissions - - // Generate fixed point equivalent of `retryCount = max(0,n−MRP_BACKOFF_THRESHOLD)` - int retryCount = sendCount - MRP_BACKOFF_THRESHOLD; - if (retryCount < 0) - retryCount = 0; // Enforce floor - if (retryCount > 4) - retryCount = 4; // Enforce reasonable maximum after 5 tries - - // Generate fixed point equivalent of `backoff = i⋅1.6^retryCount` + constexpr uint32_t MRP_BACKOFF_JITTER_BASE = 1024; + // MRP_BACKOFF_BASE = 1.6; see section "4.11.8. Parameters and Constants" + constexpr uint32_t MRP_BACKOFF_BASE_NUMERATOR = 16; + constexpr uint32_t MRP_BACKOFF_BASE_DENOMINATOR = 10; + constexpr int MRP_BACKOFF_THRESHOLD = 1; + + // Implement: + // mrpBackoffTime = i * MRP_BACKOFF_BASE^(max(0,n-MRP_BACKOFF_THRESHOLD)) * (1.0 + random(0,1) * MRP_BACKOFF_JITTER) + // from section "4.11.2.1. Retransmissions", where: + // i == baseInterval + // n == sendCount + + // Calculate exponent `max(0,n−MRP_BACKOFF_THRESHOLD)` + int exponent = sendCount - MRP_BACKOFF_THRESHOLD; + if (exponent < 0) + exponent = 0; // Enforce floor + if (exponent > 4) + exponent = 4; // Enforce reasonable maximum after 5 tries + + // Calculate `mrpBackoffTime = i * MRP_BACKOFF_BASE^(max(0,n-MRP_BACKOFF_THRESHOLD))` uint32_t backoffNum = 1; uint32_t backoffDenom = 1; - for (int i = 0; i < retryCount; i++) + + for (int i = 0; i < exponent; i++) { backoffNum *= MRP_BACKOFF_BASE_NUMERATOR; - backoffDenom *= MRP_BACKOFF_BASE_DENOMENATOR; + backoffDenom *= MRP_BACKOFF_BASE_DENOMINATOR; } - backoff = backoff * backoffNum / backoffDenom; - // Implement jitter scaler: `t *= (1.0+random(0,1)⋅MRP_BACKOFF_JITTER)` - // where jitter is random multiplier from 1.000 to 1.250: + System::Clock::Timestamp mrpBackoffTime = baseInterval * backoffNum / backoffDenom; + + // Multiply `mrpBackoffTime` by `(1.0 + random(0,1) * MRP_BACKOFF_JITTER)`, + // where MRP_BACKOFF_JITTER = 0.25; see section "4.11.8. Parameters and Constants". uint32_t jitter = MRP_BACKOFF_JITTER_BASE + Crypto::GetRandU8(); - backoff = backoff * jitter / MRP_BACKOFF_JITTER_BASE; + mrpBackoffTime = mrpBackoffTime * jitter / MRP_BACKOFF_JITTER_BASE; - return backoff; + return mrpBackoffTime; } void ReliableMessageMgr::StartRetransmision(RetransTableEntry * entry) diff --git a/src/messaging/ReliableMessageMgr.h b/src/messaging/ReliableMessageMgr.h index ca3a9252e2f255..889d236746732c 100644 --- a/src/messaging/ReliableMessageMgr.h +++ b/src/messaging/ReliableMessageMgr.h @@ -101,7 +101,7 @@ class ReliableMessageMgr /** * Calculate the backoff timer for the retransmission. * - * @param[in] backoffBase The base interval to use for the backoff calculation, either the active or idle interval. + * @param[in] baseInterval The base interval to use for the backoff calculation, either the active or idle interval. * @param[in] sendCount Count of how many times this message * has been retransmitted so far (0 if it has * been sent only once with no retransmits, @@ -109,7 +109,7 @@ class ReliableMessageMgr * * @retval The backoff time value, including jitter. */ - static System::Clock::Timestamp GetBackoff(System::Clock::Timestamp backoffBase, uint8_t sendCount); + static System::Clock::Timestamp GetBackoff(System::Clock::Timestamp baseInterval, uint8_t sendCount); /** * Start retranmisttion of cached encryped packet for current entry.