2016-08-05 20:13:33 +08:00
|
|
|
/** @file
|
|
|
|
* @brief Trickle timer library
|
|
|
|
*
|
|
|
|
* This implements Trickle timer as specified in RFC 6206
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-08-05 20:13:33 +08:00
|
|
|
*/
|
|
|
|
|
2022-05-06 17:12:04 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2018-11-30 18:54:56 +08:00
|
|
|
LOG_MODULE_REGISTER(net_trickle, CONFIG_NET_TRICKLE_LOG_LEVEL);
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
2022-05-06 17:12:04 +08:00
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/random/rand32.h>
|
2016-08-05 20:13:33 +08:00
|
|
|
|
2022-05-06 17:12:04 +08:00
|
|
|
#include <zephyr/net/net_core.h>
|
|
|
|
#include <zephyr/net/trickle.h>
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
#define TICK_MAX ~0
|
|
|
|
|
2019-04-24 17:09:37 +08:00
|
|
|
static void trickle_timeout(struct k_work *work);
|
|
|
|
|
2016-08-05 20:13:33 +08:00
|
|
|
static inline bool is_suppression_disabled(struct net_trickle *trickle)
|
|
|
|
{
|
|
|
|
return trickle->k == NET_TRICKLE_INFINITE_REDUNDANCY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool is_tx_allowed(struct net_trickle *trickle)
|
|
|
|
{
|
|
|
|
return is_suppression_disabled(trickle) ||
|
|
|
|
(trickle->c < trickle->k);
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline uint32_t get_end(struct net_trickle *trickle)
|
2016-08-05 20:13:33 +08:00
|
|
|
{
|
|
|
|
return trickle->Istart + trickle->I;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns a random time point t in [I/2 , I) */
|
2020-05-28 00:26:57 +08:00
|
|
|
static uint32_t get_t(uint32_t I)
|
2016-08-05 20:13:33 +08:00
|
|
|
{
|
|
|
|
I >>= 1;
|
|
|
|
|
|
|
|
NET_DBG("[%d, %d)", I, I << 1);
|
|
|
|
|
|
|
|
return I + (sys_rand32_get() % I);
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:31:17 +08:00
|
|
|
static void double_interval_timeout(struct net_trickle *trickle)
|
2016-08-05 20:13:33 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t rand_time;
|
|
|
|
uint32_t last_end = get_end(trickle);
|
2016-08-05 20:13:33 +08:00
|
|
|
|
2019-03-27 09:57:45 +08:00
|
|
|
trickle->c = 0U;
|
2016-08-05 20:13:33 +08:00
|
|
|
|
2016-11-10 04:15:19 +08:00
|
|
|
NET_DBG("now %u (was at %u)", k_uptime_get_32(), last_end);
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
/* Check if we need to double the interval */
|
|
|
|
if (trickle->I <= (trickle->Imax_abs >> 1)) {
|
|
|
|
/* Double if I <= Imax/2 */
|
|
|
|
trickle->I <<= 1;
|
|
|
|
|
|
|
|
NET_DBG("double I %u", trickle->I);
|
|
|
|
} else {
|
|
|
|
trickle->I = trickle->Imax_abs;
|
|
|
|
|
|
|
|
NET_DBG("I %u", trickle->I);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Random t in [I/2, I) */
|
|
|
|
rand_time = get_t(trickle->I);
|
|
|
|
|
|
|
|
NET_DBG("doubling time %u", rand_time);
|
|
|
|
|
2016-11-10 04:15:19 +08:00
|
|
|
trickle->Istart = k_uptime_get_32() + rand_time;
|
2021-04-26 21:31:17 +08:00
|
|
|
trickle->double_to = false;
|
|
|
|
|
2021-03-31 23:31:30 +08:00
|
|
|
k_work_reschedule(&trickle->timer, K_MSEC(rand_time));
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
NET_DBG("last end %u new end %u for %u I %u",
|
|
|
|
last_end, get_end(trickle), trickle->Istart, trickle->I);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void reschedule(struct net_trickle *trickle)
|
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t now = k_uptime_get_32();
|
|
|
|
uint32_t diff = get_end(trickle) - now;
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
NET_DBG("now %d end in %d", now, diff);
|
|
|
|
|
|
|
|
/* Did the clock wrap */
|
2020-05-28 00:26:57 +08:00
|
|
|
if ((int32_t)diff < 0) {
|
2018-11-30 03:23:03 +08:00
|
|
|
diff = 0U;
|
2016-08-05 20:13:33 +08:00
|
|
|
NET_DBG("Clock wrap");
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:31:17 +08:00
|
|
|
trickle->double_to = true;
|
|
|
|
|
2021-03-31 23:31:30 +08:00
|
|
|
k_work_reschedule(&trickle->timer, K_MSEC(diff));
|
2016-08-05 20:13:33 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 21:31:17 +08:00
|
|
|
static void inteval_timeout(struct net_trickle *trickle)
|
2016-08-05 20:13:33 +08:00
|
|
|
{
|
2016-11-10 04:15:19 +08:00
|
|
|
NET_DBG("Trickle timeout at %d", k_uptime_get_32());
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
if (trickle->cb) {
|
|
|
|
NET_DBG("TX ok %d c(%u) < k(%u)",
|
|
|
|
is_tx_allowed(trickle), trickle->c, trickle->k);
|
|
|
|
|
|
|
|
trickle->cb(trickle, is_tx_allowed(trickle),
|
|
|
|
trickle->user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (net_trickle_is_running(trickle)) {
|
|
|
|
reschedule(trickle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:31:17 +08:00
|
|
|
static void trickle_timeout(struct k_work *work)
|
|
|
|
{
|
2022-01-19 12:07:51 +08:00
|
|
|
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
|
|
|
struct net_trickle *trickle = CONTAINER_OF(dwork,
|
2021-04-26 21:31:17 +08:00
|
|
|
struct net_trickle,
|
|
|
|
timer);
|
|
|
|
|
|
|
|
if (trickle->double_to) {
|
|
|
|
double_interval_timeout(trickle);
|
|
|
|
} else {
|
|
|
|
inteval_timeout(trickle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 20:13:33 +08:00
|
|
|
static void setup_new_interval(struct net_trickle *trickle)
|
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t t;
|
2016-08-05 20:13:33 +08:00
|
|
|
|
2019-03-27 09:57:45 +08:00
|
|
|
trickle->c = 0U;
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
t = get_t(trickle->I);
|
|
|
|
|
2016-11-10 04:15:19 +08:00
|
|
|
trickle->Istart = k_uptime_get_32();
|
2016-08-05 20:13:33 +08:00
|
|
|
|
2021-03-31 23:31:30 +08:00
|
|
|
k_work_reschedule(&trickle->timer, K_MSEC(t));
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
NET_DBG("new interval at %d ends %d t %d I %d",
|
|
|
|
trickle->Istart,
|
|
|
|
get_end(trickle),
|
|
|
|
t,
|
|
|
|
trickle->I);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_IMIN(Imin) \
|
|
|
|
((Imin < 2) || (Imin > (TICK_MAX >> 1)))
|
|
|
|
|
|
|
|
int net_trickle_create(struct net_trickle *trickle,
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t Imin,
|
|
|
|
uint8_t Imax,
|
|
|
|
uint8_t k)
|
2016-08-05 20:13:33 +08:00
|
|
|
{
|
|
|
|
NET_ASSERT(trickle && Imax > 0 && k > 0 && !CHECK_IMIN(Imin));
|
|
|
|
|
2018-09-12 10:09:03 +08:00
|
|
|
(void)memset(trickle, 0, sizeof(struct net_trickle));
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
trickle->Imin = Imin;
|
|
|
|
trickle->Imax = Imax;
|
|
|
|
trickle->Imax_abs = Imin << Imax;
|
|
|
|
trickle->k = k;
|
|
|
|
|
2017-07-26 18:35:53 +08:00
|
|
|
NET_ASSERT(trickle->Imax_abs);
|
|
|
|
|
2016-08-05 20:13:33 +08:00
|
|
|
NET_DBG("Imin %d Imax %u k %u Imax_abs %d",
|
|
|
|
trickle->Imin, trickle->Imax, trickle->k,
|
|
|
|
trickle->Imax_abs);
|
|
|
|
|
2021-03-31 23:31:30 +08:00
|
|
|
k_work_init_delayable(&trickle->timer, trickle_timeout);
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_trickle_start(struct net_trickle *trickle,
|
|
|
|
net_trickle_cb_t cb,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
NET_ASSERT(trickle && cb);
|
|
|
|
|
|
|
|
trickle->cb = cb;
|
|
|
|
trickle->user_data = user_data;
|
2021-04-26 21:31:17 +08:00
|
|
|
trickle->double_to = false;
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
/* Random I in [Imin , Imax] */
|
|
|
|
trickle->I = trickle->Imin +
|
|
|
|
(sys_rand32_get() % (trickle->Imax_abs - trickle->Imin + 1));
|
|
|
|
|
|
|
|
setup_new_interval(trickle);
|
|
|
|
|
|
|
|
NET_DBG("start %d end %d in [%d , %d)",
|
|
|
|
trickle->Istart, get_end(trickle),
|
|
|
|
trickle->I >> 1, trickle->I);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_trickle_stop(struct net_trickle *trickle)
|
|
|
|
{
|
|
|
|
NET_ASSERT(trickle);
|
|
|
|
|
2021-03-31 23:31:30 +08:00
|
|
|
k_work_cancel_delayable(&trickle->timer);
|
2016-08-05 20:13:33 +08:00
|
|
|
|
2019-03-27 09:57:45 +08:00
|
|
|
trickle->I = 0U;
|
2016-08-05 20:13:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_trickle_consistency(struct net_trickle *trickle)
|
|
|
|
{
|
|
|
|
NET_ASSERT(trickle);
|
|
|
|
|
|
|
|
if (trickle->c < 0xFF) {
|
|
|
|
trickle->c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_DBG("consistency %u", trickle->c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_trickle_inconsistency(struct net_trickle *trickle)
|
|
|
|
{
|
|
|
|
NET_ASSERT(trickle);
|
|
|
|
|
|
|
|
if (trickle->I != trickle->Imin) {
|
|
|
|
NET_DBG("inconsistency");
|
|
|
|
|
|
|
|
trickle->I = trickle->Imin;
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_new_interval(trickle);
|
|
|
|
}
|