Skip to content

Commit

Permalink
gnrc_ipv6_nib: sync RTT64 with RA timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed May 30, 2022
1 parent df90bf1 commit 67f000e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
4 changes: 4 additions & 0 deletions sys/net/gnrc/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ ifneq (,$(filter gnrc_ipv6_nib_router,$(USEMODULE)))
USEMODULE += gnrc_ipv6_nib
endif

ifneq (,$(filter gnrc_ipv6_nib_timestamp,$(USEMODULE)))
USEMODULE += rtt64
endif

ifneq (,$(filter gnrc_ipv6_nib,$(USEMODULE)))
DEFAULT_MODULE += auto_init_gnrc_ipv6_nib
USEMODULE += evtimer
Expand Down
36 changes: 31 additions & 5 deletions sys/net/gnrc/network_layer/ipv6/nib/nib.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "net/dhcpv6/client.h"
#endif

#if IS_USED(MODULE_RTT64)
#include "rtt64.h"
#endif

#include "_nib-internal.h"
#include "_nib-arsm.h"
#include "_nib-router.h"
Expand Down Expand Up @@ -1672,11 +1676,33 @@ static void _handle_timestamp(const ndp_opt_timestamp_t *tsmp)
return;
}

uint64_t seconds = byteorder_ntohll(tsmp->timestamp) >> 16;
uint16_t ms = (1000 * (seconds & 0xFFFF)) >> 16;

uint64_t now = byteorder_ntohll(tsmp->timestamp);
DEBUG("nib: received Timestamp option:\n");
DEBUG(" - Seconds: %u\n", (unsigned)seconds);
DEBUG(" - Milliseconds: %u\n", ms);
DEBUG(" - Seconds: %u\n", (unsigned)(now >> 16));
DEBUG(" - Milliseconds: %u\n", (unsigned)(1000 * (now & 0xFFFF)) >> 16);
#if IS_USED(MODULE_RTT64)
static uint16_t diff;
uint64_t local = rtt64_get_counter();
now += diff;

rtt64_set_counter(now);

DEBUG(" - remote: %u ticks\n", (unsigned)now);
DEBUG(" - local: %u ticks\n", (unsigned)local);
DEBUG(" - diff: %u ticks\n", diff);

/* try to account for transmission time */
/* only do diff accounting if diff is less than 1s */
if ((now < local) && (local - now < 0xFFFF)) {
/* now is already now + diff */
diff = (local - now) / 2;
DEBUG(" - new diff: %u ticks\n", diff);
} else if ((now > local) && (now - local < 0xFFFF)) {
diff -= _min(diff, now - local);
DEBUG(" - new diff: %u ticks (%u ticks too fast)\n", diff, (unsigned)(now - local));
} else {
diff = 0;
}
#endif
}
/** @} */
9 changes: 9 additions & 0 deletions sys/net/gnrc/network_layer/ndp/gnrc_ndp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "net/gnrc/sixlowpan/nd.h"
#endif
#include "net/ndp.h"
#include "rtt64.h"

#include "net/gnrc/ndp.h"

Expand Down Expand Up @@ -529,6 +530,14 @@ void gnrc_ndp_rtr_adv_send(gnrc_netif_t *netif, const ipv6_addr_t *src,
}
pkt = hdr;
}
if (IS_USED(MODULE_GNRC_IPV6_NIB_TIMESTAMP) && IS_USED(MODULE_RTT64)) {
uint64_t now = rtt64_get_counter();
if ((hdr = gnrc_ndp_opt_timestamp_build(now, pkt)) == NULL) {
DEBUG("ndp rtr: no space left in packet buffer\n");
break;
}
pkt = hdr;
}
if (src == NULL) {
/* get address from source selection algorithm.
* Only link local addresses may be used (RFC 4861 section 4.1) */
Expand Down

0 comments on commit 67f000e

Please sign in to comment.