($INBOX_DIR/description missing)
 help / color / mirror / Atom feed
From: Denis Kenzior <denkenz at gmail.com>
To: ell at lists.01.org
Subject: Re: [PATCH] dhcp,dhcp6,icmp6,time: Convert timestamps to CLOCK_BOOTTIME
Date: Tue, 17 May 2022 09:37:06 -0500	[thread overview]
Message-ID: <bc24661a-7fcb-01f7-62de-da38f82b9e34@gmail.com> (raw)
In-Reply-To: 20220516213153.1612039-1-andrew.zaborowski@intel.com

[-- Attachment #1: Type: text/plain, Size: 3974 bytes --]

Hi Andrew,

On 5/16/22 16:31, Andrew Zaborowski wrote:
> 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(-)
> 

Makes sense to me, just minor comments:

> diff --git a/ell/dhcp-transport.c b/ell/dhcp-transport.c
> index 52da2db..ba1423f 100644
> --- a/ell/dhcp-transport.c
> +++ b/ell/dhcp-transport.c
> @@ -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();

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

>   		}
>   	}
>   

<snip>

> diff --git a/ell/time.c b/ell/time.c
> index d02dabe..cda09be 100644
> --- a/ell/time.c
> +++ b/ell/time.c
> @@ -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?

> +{
> +	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);

> +	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.

> +}
> 

Regards,
-Denis

             reply	other threads:[~2022-05-17 14:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-17 14:37 Denis Kenzior [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-05-18 14:22 [PATCH] dhcp,dhcp6,icmp6,time: Convert timestamps to CLOCK_BOOTTIME Denis Kenzior
2022-05-18 14:19 Denis Kenzior
2022-05-18  0:35 Andrew Zaborowski
2022-05-18  0:22 [PATCH] dhcp, dhcp6, icmp6, time: " Andrew Zaborowski
2022-05-17 23:29 Andrew Zaborowski
2022-05-16 21:31 [PATCH] dhcp,dhcp6,icmp6,time: " Andrew Zaborowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bc24661a-7fcb-01f7-62de-da38f82b9e34@gmail.com \
    --to=ell@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).