Skip to contentSkip to conversation

dhcp,dhcp6,icmp6,time: Convert timestamps to CLOCK_BOOTTIME

PATCH In review Andrew Zaborowski 2 participants 5 messages May 16, 2022 – May 18, 2022

lore
Share & tools
lore

Andrew Zaborowski

author
#0 lore raw

PATCH 1/1 dhcp,dhcp6,icmp6,time: Convert timestamps to CLOCK_BOOTTIME

The frame reception timestamps obtained for DHCPv4, DHCPv6 and ICMPv6 frames from the kernel are based on CLOCK_REALTIME, there's no option to switch them to CLOCK_BOOTTIME, which we use across our retransmission and renew logic. Add a time-private.h utility to get the offset between the two clocks and offset the timestamps right where read them from recvmsg() to produce CLOCK_BOOTTIME based times. This isn't 100% bulletproof because the offset between the two clocks can vary between the moment the frame was received by the kernel and the time we read the offset. The probability is very low though and we have no better solution at this time other than fixing it in the kernel. Using CLOCK_REALTIME for frame reception timestamps seems to only create potential for problems in the common usages.

Fixes c78ad1bb6d7e ("dhcp: Set lease expiry based on frame reception times")
---
 ell/dhcp-transport.c  |  8 +++++++-
 ell/dhcp6-transport.c | 12 +++++++++---
 ell/icmp6.c           |  7 ++++++-
 ell/time-private.h    |  1 +
 ell/time.c            | 17 +++++++++++++++++
 5 files changed, 40 insertions(+), 5 deletions(-)
ell/dhcp-transport.c +7 -1
index 52da2db..ba1423f 100644
@@ -45,6 +45,7 @@
 #include "util.h"
 #include "private.h"
 #include "time.h"
+#include "time-private.h"
 #include "dhcp-private.h"
 
 struct dhcp_default_transport {
@@ -170,7 +171,12 @@ static bool _dhcp_default_transport_read_handler(struct l_io *io,
 				CMSG_LEN(sizeof(struct timeval))) {
 			const struct timeval *tv = (void *) CMSG_DATA(cmsg);
 
-			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec;
+			/*
+			 * Not ideal but this is our best approximation of the
+			 * CLOCK_BOOTTIME-based Rx time.
+			 */
+			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec -
+				_time_get_realtime_boottime_offset();
 		}
 	}
 
ell/dhcp6-transport.c +9 -3
index 13545b8..2bbdf8e 100644
@@ -40,6 +40,7 @@
 #include "missing.h"
 #include "io.h"
 #include "time.h"
+#include "time-private.h"
 #include "dhcp6-private.h"
 
 struct dhcp6_default_transport {
@@ -81,9 +82,14 @@ static bool _dhcp6_default_transport_read_handler(struct l_io *io,
 				cmsg->cmsg_type == SCM_TIMESTAMP &&
 				cmsg->cmsg_len ==
 				CMSG_LEN(sizeof(struct timeval))) {
-		const struct timeval *tv = (void *) CMSG_DATA(cmsg);
-
-			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec;
+			const struct timeval *tv = (void *) CMSG_DATA(cmsg);
+
+			/*
+			 * Not ideal but this is our best approximation of the
+			 * CLOCK_BOOTTIME-based Rx time.
+			 */
+			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec -
+				_time_get_realtime_boottime_offset();
 		}
 	}
 
ell/icmp6.c +6 -1
index 96ba1ec..56019fc 100644
@@ -249,7 +249,12 @@ static int icmp6_receive(int s, void *buf, size_t buf_len, struct in6_addr *src,
 				CMSG_LEN(sizeof(struct timeval))) {
 			const struct timeval *tv = (void *) CMSG_DATA(cmsg);
 
-			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec;
+			/*
+			 * Not ideal but this is our best approximation of the
+			 * CLOCK_BOOTTIME-based Rx time.
+			 */
+			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec -
+				_time_get_realtime_boottime_offset();
 		}
 	}
 
ell/time-private.h +1 -0
index dc4d6c2..8ff581b 100644
@@ -23,3 +23,4 @@
 uint64_t _time_pick_interval_secs(uint32_t min_secs, uint32_t max_secs);
 uint64_t _time_fuzz_msecs(uint64_t ms);
 uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset);
+int64_t _time_get_realtime_boottime_offset(void);
ell/time.c +17 -0
index d02dabe..cda09be 100644
@@ -105,3 +105,20 @@ uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset)
 
 	return ms;
 }
+
+/*
+ * Get approximate difference between CLOCK_REALTIME and CLOCK_BOOTTIME,
+ * for converting recent timestamps from one to the other.  The value can
+ * change at any time.
+ */
+int64_t _time_get_realtime_boottime_offset(void)
+{
+	struct timespec boottime;
+	struct timespec realtime;
+
+	clock_gettime(CLOCK_BOOTTIME, &boottime);
+	clock_gettime(CLOCK_REALTIME, &realtime);
+
+	return ((int64_t) realtime.tv_sec - boottime.tv_sec) * 1000000 +
+		((int64_t) realtime.tv_nsec - boottime.tv_nsec) / 1000;
+}
Signature · 1 line
2.32.0
#1 lore raw

Hi Andrew,

Quoted 13 lines · Andrew Zaborowski

The frame reception timestamps obtained for DHCPv4, DHCPv6 and ICMPv6 frames from the kernel are based on CLOCK_REALTIME, there's no option to switch them to CLOCK_BOOTTIME, which we use across our retransmission and renew logic. Add a time-private.h utility to get the offset between the two clocks and offset the timestamps right where read them from recvmsg() to produce CLOCK_BOOTTIME based times. This isn't 100% bulletproof because the offset between the two clocks can vary between the moment the frame was received by the kernel and the time we read the offset. The probability is very low though and we have no better solution at this time other than fixing it in the kernel. Using CLOCK_REALTIME for frame reception timestamps seems to only create potential for problems in the common usages.

Quoted 9 lines
Fixes c78ad1bb6d7e ("dhcp: Set lease expiry based on frame reception times")
---
  ell/dhcp-transport.c  |  8 +++++++-
  ell/dhcp6-transport.c | 12 +++++++++---
  ell/icmp6.c           |  7 ++++++-
  ell/time-private.h    |  1 +
  ell/time.c            | 17 +++++++++++++++++
  5 files changed, 40 insertions(+), 5 deletions(-)

Makes sense to me, just minor comments:

Quoted 13 lines
ell/dhcp-transport.c +1 -0
index 52da2db..ba1423f 100644
@@ -45,6 +45,7 @@
  #include "util.h"
  #include "private.h"
  #include "time.h"
+#include "time-private.h"
  #include "dhcp-private.h"
 
  struct dhcp_default_transport {

ell/dhcp-transport.c static bool _dhcp_default_transport_read_handler(struct l_io *io, · line 179

Quoted 10 lines
ell/dhcp-transport.c +6 -1
@@ -170,7 +171,12 @@ static bool _dhcp_default_transport_read_handler(struct l_io *io,
  				CMSG_LEN(sizeof(struct timeval))) {
  			const struct timeval *tv = (void *) CMSG_DATA(cmsg);
 
-			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec;
+			/*
+			 * Not ideal but this is our best approximation of the
+			 * CLOCK_BOOTTIME-based Rx time.
+			 */
+			timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec -
+				_time_get_realtime_boottime_offset();

Can we actually just send the timestamp as a 'struct timeval' to the private function? Instead of duplicating the conversion calculation everywhere?

Quoted 3 lines
ell/dhcp-transport.c +0 -0
  		}
  	}
 

<snip>

Quoted 5 lines
ell/time.c +0 -0
index d02dabe..cda09be 100644

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 114

Quoted 10 lines
ell/time.c +7 -0
@@ -105,3 +105,20 @@ uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset)
 
  	return ms;
  }
+
+/*
+ * Get approximate difference between CLOCK_REALTIME and CLOCK_BOOTTIME,
+ * for converting recent timestamps from one to the other.  The value can
+ * change at any time.
+ */
+int64_t _time_get_realtime_boottime_offset(void)

So I would avoid signed integers for these calculations. We already have l_time_before/l_time_after. And l_time_offset takes care of overflows. Lets use those to implement this. You also need to consider potential underflow, though it is probably astronomically unlikely.

I'd name this something like:

uint64_t _time_realtime_to_boottime(const struct timeval *ts)

By the way, given that you're using struct timespec below, do you want to use SO_TIMESTAMPNS?

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 121

Quoted 7 lines
ell/time.c +7 -0
+{
+	struct timespec boottime;
+	struct timespec realtime;
+
+	clock_gettime(CLOCK_BOOTTIME, &boottime);
+	clock_gettime(CLOCK_REALTIME, &realtime);
+

Then here do something like:

offset = l_time_diff(ts, now_realtime);

/* most likely case, timestamp in the past */

if (l_time_before(ts, now_realtime)) {
	if (offset > now_boot)
		return 0;

	return now_boot - offset;
}

return l_time_offset(now_boot, offset);

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 123

Quoted 2 lines
ell/time.c +2 -0
+	return ((int64_t) realtime.tv_sec - boottime.tv_sec) * 1000000 +
+		((int64_t) realtime.tv_nsec - boottime.tv_nsec) / 1000;

Lets use the L_USEC_* defines when possible.

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 124

Quoted 2 lines
ell/time.c +1 -0
+}

Regards,
-Denis

author
#2 lore raw

Re: [PATCH] dhcp, dhcp6, icmp6, time: Convert timestamps to CLOCK_BOOTTIME

Hi Denis,

Quoted 10 lines · Denis Kenzior

ell/dhcp-transport.c static bool _dhcp_default_transport_read_handler(struct l_io *io, · line 179

Quoted 8 lines · Andrew Zaborowski
ell/dhcp-transport.c +6 -1
-                     timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec;
+                     /*
+                      * Not ideal but this is our best approximation of the
+                      * CLOCK_BOOTTIME-based Rx time.
+                      */
+                     timestamp = tv->tv_sec * L_USEC_PER_SEC + tv->tv_usec -
+                             _time_get_realtime_boottime_offset();
Quoted 2 lines

Can we actually just send the timestamp as a 'struct timeval' to the private function? Instead of duplicating the conversion calculation everywhere?

Ok. (The idea was that having the offset you can convert in either direction)

Quoted 13 lines
Quoted 4 lines
              }
      }

<snip>

Quoted 6 lines
ell/time.c +0 -0
index d02dabe..cda09be 100644
@@ -105,3 +105,20 @@ uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset)
 
Quoted 14 lines

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 114

Quoted 9 lines
ell/time.c +7 -0
      return ms;
  }
+
+/*
+ * Get approximate difference between CLOCK_REALTIME and CLOCK_BOOTTIME,
+ * for converting recent timestamps from one to the other.  The value can
+ * change at any time.
+ */
+int64_t _time_get_realtime_boottime_offset(void)

So I would avoid signed integers for these calculations. We already have l_time_before/l_time_after. And l_time_offset takes care of overflows. Lets use those to implement this. You also need to consider potential underflow, though it is probably astronomically unlikely.

Either int64_t or uin64_t should have worked for overflows and underflows as long as we operate on 64-bit numbers throughout.

Quoted 7 lines

I'd name this something like:

uint64_t _time_realtime_to_boottime(const struct timeval *ts)

By the way, given that you're using struct timespec below, do you want to use SO_TIMESTAMPNS?

It doesn't seem that this would simplify the arithmetics in this case. I wonder if we should bother to use the _NEW variant, which makes sure the timestamp is year 2038-proof on 32-bit systems.

Quoted 14 lines

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 121

Quoted 8 lines
ell/time.c +7 -0
+{
+     struct timespec boottime;
+     struct timespec realtime;
+
+     clock_gettime(CLOCK_BOOTTIME, &boottime);
+     clock_gettime(CLOCK_REALTIME, &realtime);
+

Then here do something like:

offset = l_time_diff(ts, now_realtime);

/* most likely case, timestamp in the past */

Quoted 8 lines
if (l_time_before(ts, now_realtime)) {
        if (offset > now_boot)
                return 0;

        return now_boot - offset;
}

return l_time_offset(now_boot, offset);

This is a complicated way to perform two subtractions but ok ;-)

Quoted 5 lines

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 123

Quoted 3 lines
ell/time.c +2 -0
+     return ((int64_t) realtime.tv_sec - boottime.tv_sec) * 1000000 +
+             ((int64_t) realtime.tv_nsec - boottime.tv_nsec) / 1000;

Lets use the L_USEC_* defines when possible.

Ok. (IIRC this was done for consistency with l_time_now)

Best regards

author
#3 lore raw

Re: [PATCH] dhcp, dhcp6, icmp6, time: Convert timestamps to CLOCK_BOOTTIME

Quoted 8 lines · Andrew Zaborowski
Quoted 4 lines · Denis Kenzior

So I would avoid signed integers for these calculations. We already have l_time_before/l_time_after. And l_time_offset takes care of overflows. Lets use those to implement this. You also need to consider potential underflow, though it is probably astronomically unlikely.

Either int64_t or uin64_t should have worked for overflows and underflows as long as we operate on 64-bit numbers throughout.

Sorry, I didn't get which kind of overflow/underflow you meant. You could indeed have CLOCK_REALTIME-based timestamps that can't be represented in CLOCK_BOOTTIME without an overflow or an underflow.

The resulting wrapped value would still work for calculating intervals between frames, less so for expiry times if they still end up being negative.

Best regards

#4 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 7 lines
Quoted 2 lines

By the way, given that you're using struct timespec below, do you want to use SO_TIMESTAMPNS?

It doesn't seem that this would simplify the arithmetics in this case. I wonder if we should bother to use the _NEW variant, which makes sure the timestamp is year 2038-proof on 32-bit systems.

Probably might as well.

<snip>

Quoted 7 lines
Quoted 4 lines

ell/time.c uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset) · line 123

Quoted 2 lines
ell/time.c +2 -0
+     return ((int64_t) realtime.tv_sec - boottime.tv_sec) * 1000000 +
+             ((int64_t) realtime.tv_nsec - boottime.tv_nsec) / 1000;

Lets use the L_USEC_* defines when possible.

Ok. (IIRC this was done for consistency with l_time_now)

Ah, I need to fix that. The problem is that this function was added before the #defines existed.

I have now added commit:
bd5a2dc26441 ("time: Add converters from struct {timespec|timeval}")

Please review.

Regards,
-Denis