Fix update_time() and openvpn_gettimeofday()
PATCH 1/1 Fix update_time() and openvpn_gettimeofday()
With TIME_BACKTRACK_PROTECTION defined, openvpn_gettimeofday() uses and updates global variable "now_usec" along with "now" only if current time is ahead of the previsouly stored, taking nanoseconds into account. But, update_time() function updates only "now" leaving "now_usec" as is with any previously value stored.
This breaks both update_time() and openvpn_gettimeofday() results and leads to time jumps in the future within one second, can affect shaper and user timers.
100.900 openvpn_gettimeofday():
now set to 100s, now_usec set to 100ns, stored time is 100.900
101.300 update_time():
now set to 101s, but now_usec is not updated and still 900ns, stored time jumps to the future 101.900
101.600 openvpn_gettimeofday():
current time 101.600 is in the past relatively stored time 101.900, now & now_usec variables are not updated, returned time 101.900 is still and again incorrect
102.100 openvpn_gettimeofday():
current time 102.100 is no longer in the past relatively stored time 101.900, so now & now_usec get updated with wrong time delta from previous openvpn_gettimeofday() call or now/now_usec math
Since update_time() and openvpn_gettimeofday() calls are mixed in runtime, to fix their coexistance update_time() must update "now_usec" as well, calling just update_now() is not enough.
--- src/openvpn/otime.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
src/openvpn/otime.h +1 -5
@@ -78,13 +78,9 @@ openvpn_gettimeofday(struct timeval *tv, void *tz)static inline void update_time (void ) { -#ifdef _WIN32 -/* on _WIN32, gettimeofday is faster than time(NULL) */ +/* can't use time(NULL), now_usec needs to be updated */ struct timeval tv;openvpn_gettimeofday (&tv,NULL ); -#else -update_now (time (NULL )); -#endif }#else /* !TIME_BACKTRACK_PROTECTION */
Signature · 1 line
2.17.1
Quoted 9 lines
src/openvpn/otime.h +0
-1@@ -78,13 +78,9 @@ openvpn_gettimeofday(struct timeval *tv, void *tz)static inline void update_time (void ) { -#ifdef _WIN32
src/openvpn/otime.h openvpn_gettimeofday(struct timeval *tv, void *tz) · line 86
Quoted 10 lines
src/openvpn/otime.h +1
-4-/* on _WIN32, gettimeofday is faster than time(NULL) */ +/* can't use time(NULL), now_usec needs to be updated */ struct timeval tv; openvpn_gettimeofday(&tv, NULL); -#else -update_now (time (NULL )); -#endif }#else /* !TIME_BACKTRACK_PROTECTION */
I am hesitant about this change, it mentions that gettimeofday is faster on Windows than time(NULL) and we prefer it there for this reason. But that also implies it is the other way round on other platforms. The update_time function is called in multiple critical places. Do we have any idea about this? I remember vaguely that the default gettimeofday in FreeBSD is more accurate than the Linux variant (involves a kernel call) but you can request a version similar to the Linux one by some flag (reading from a shared readonly memory page that is read only).
Arne
Hi Arne,
Quoted 3 lines
But that also
implies it is the other way round on other platforms. Theupdate_timefunction is called in multiple critical places. Do we have any idea about this?
On Linux with glibc's implementation both time() and gettimeofday() results in the same clock_gettime() call over VDSO (https://man7.org/linux/man-pages/man7/vdso.7.html).
On FreeBSD (and so), same if VDSO is used both time() and gettimeofday() will use clock_gettime too with even additional "tv->tv_usec = 0" for time(). So, on most of architectures there will be no difference after replacing time() to gettimeofday() except. asm commands for moving 32/64bit tv_usec, it'll be not visible. On some acrhs/systems where VDSO is not supported - same syscall will happen.
Quoted 5 lines
I remember
vaguely that the default gettimeofday in FreeBSD is more accurate than the Linux variant (involves a kernel call) but you can request a version similar to the Linux one by some flag (reading from a shared readonly memory page that is read only).
As for clock drifting, it maybe feasible to use clock_gettime() with CLOCK_MONOTONIC and get rid of homegrown time adj code at all -> returned time will always be monotonic by design. At least on supported platforms (!_WIN32).
--
Best Regards, Vladislav Grishenko
Quoted 38 lines
-----Original Message-----
From: Arne Schwabe <[email protected]...>
Sent: Tuesday, September 22, 2020 1:41 PM
To: Vladislav Grishenko <[email protected]...>; openvpn- [email protected]
Subject: Re: [Openvpn-devel] [PATCH] Fixupdate_time()andopenvpn_gettimeofday()Quoted 8 lines
src/openvpn/otime.h +0
-0@@ -78,13 +78,9 @@ openvpn_gettimeofday(struct timeval *tv, void *tz)static inline void
update_time(void)
{src/openvpn/otime.h +0
-1-#ifdef _WIN32src/openvpn/otime.h openvpn_gettimeofday(struct timeval *tv, void *tz) · line 86
Quoted 10 lines
src/openvpn/otime.h +1
-4-/* on _WIN32, gettimeofday is faster than time(NULL) */ +/* can't use time(NULL), now_usec needs to be updated */ struct timeval tv; openvpn_gettimeofday(&tv, NULL); -#else -update_now (time (NULL )); -#endif }#else /* !TIME_BACKTRACK_PROTECTION */ I am hesitant about this change, it mentions that gettimeofday is faster on Windows than time(NULL) and we prefer it there for this reason. But that also implies it is the other way round on other platforms. The
update_timefunction is called in multiple critical places. Do we have any idea about this? I remember vaguely that the default gettimeofday in FreeBSD is more accurate than the Linux variant (involves a kernel call) but you can request a version similar to the Linux one by some flag (reading from a shared readonly memory page that is read only).Arne
PATCH v2 1/1 Fix update_time() and openvpn_gettimeofday() coexistance
In reply to Arne SchwabeWith TIME_BACKTRACK_PROTECTION defined, openvpn_gettimeofday() uses and updates global variable "now_usec" along with "now" only if current time is ahead of the previsouly stored, taking nanoseconds into account. But, update_time() function updates only "now" leaving "now_usec" as is with any previously value stored.
This breaks both update_time() and openvpn_gettimeofday() results and leads to time jumps in the future within one second, can affect shaper and user timers.
100.900 openvpn_gettimeofday():
now set to 100s, now_usec set to 900ns, stored time is 100.900
101.300 update_time():
now set to 101s, but now_usec is not updated and still 900ns, stored time jumps to the future 101.900
101.600 openvpn_gettimeofday():
current time 101.600 is in the past relatively stored time 101.900, now & now_usec variables are not updated, returned time 101.900 is still and again incorrect
102.100 openvpn_gettimeofday():
current time 102.100 is no longer in the past relatively stored time 101.900, so now & now_usec get updated with wrong time delta from previous openvpn_gettimeofday() call or now/now_usec math
Since update_time() and openvpn_gettimeofday() calls are mixed in runtime, there're two ways to fix their coexistance:
* either fix update_time() to always update "now_usec" for example by moving update_time() to gettimeofday(). Unfortunately, it may bring performance penalty depending on Arch/OS and vuruallized/hw environmant according the tests on various Linux, FreeBSD and MacOS versions.
* or, for now, allow update_time() to break "now_usec" value, since it's used directly only in time ajusting and always invalidate it in openvpn_gettimeofday() w/o checking the previous and possibly obsolete value with no performance changes against the current implementation.
This patch implements the second way.
--- src/openvpn/otime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/otime.c +1 -1
@@ -73,7 +73,7 @@ update_now_usec(struct timeval *tv) {const time_t last = now;update_now (tv->tv_sec); -if (now > last || (now == last && tv->tv_usec > now_usec)) +if (now >= last) { now_usec = tv->tv_usec; }
Signature · 1 line
2.17.1
PATCH v3 1/1 Fix update_time() and openvpn_gettimeofday() coexistence
In reply to Arne SchwabeWith TIME_BACKTRACK_PROTECTION defined, openvpn_gettimeofday() uses and updates global variable "now_usec" along with "now" only if current time is ahead of the previsouly stored, taking nanoseconds into account. But, update_time() function updates only "now" leaving "now_usec" as is with any previously value stored.
This breaks openvpn_gettimeofday() results and leads to time jumps in the future within one second, can affect shaper and user timers.
Example:
100.900 openvpn_gettimeofday():
now set to 100s, now_usec set to 900ns, stored time is 100.900
101.300 update_time():
now set to 101s, but now_usec is not updated and still 900ns, stored time jumps to the future 101.900
101.600 openvpn_gettimeofday():
current time 101.600 is in the past relatively stored time 101.900, now & now_usec variables are not updated, returned time 101.900 is still and again incorrect
102.100 openvpn_gettimeofday():
current time 102.100 is no longer in the past relatively stored time 101.900, so now & now_usec get updated with wrong time delta from previous openvpn_gettimeofday() call or now/now_usec math
Since update_time() and openvpn_gettimeofday() calls are mixed in runtime, there're several options to fix the things:
1. Allow update_time() to reset "now_usec" value backward to 0, since it's used directly only in time ajusting and always invalidate it in openvpn_gettimeofday() unless time has drifted backwards. Quick solution that only fixes openvpn_gettimeofday() and keeps current level of time performance and backward-protection handling way.
2. Switch update_time() to gettimeofday() not only for windows, but for all
platforms: "now_usec" will be updated accordingly. As a disadvantage, gettimeofday() may have performance penalty on older or platforms w/o VDSO where expensive kernel syscall will be made. And it will still need time adjusting code, doubt it's feasible.
3. Switch update_time() and openvpn_gettimeofday() to clock_gettime() on Linux/BSD platforms with CLOCK_REALTIME_FAST/CLOCK_REALTIME_COARSE clock sources. According tests it'll be faster with VDSO than gettimeofday() or CLOCK_REALTIME/CLOCK_REALTIME_PRECISE, but still may require adjusting code to protect from time jumps on devices with no RTC (ex. routers) where NTP is the only way to get correct time after boot. Since not every *libc have clock_gettime() and corresponding CLOCK_* defines and/or running kernel may have no VDSO/corresponding CLOCK_* support - related autotools checks and fallback code can still be necessary.
4. Switch update_time() and openvpn_gettimeofday() to clock_gettime() on Linux/BSD platforms with CLOCK_MONOTONIC_FAST/CLOCK_MONOTONIC_COARSE clock sources. This may allow to get rid of time adjusting code at all with the acceptable performance on modern systems, but may still require to fallback to gettimeofday() with adj friends on older platforms (most likely to be Linux CPE/routers). From the effort point of view, splitting the whole OpenVPN code into realtime/monotonic is most significant and desired task among the above, several parts still needs to use realtime due API or storage or output reasons.
This patch implements the first stage only.
v2: move from gettimeofday() (1st way) back to time(), don't check previous
value of "now_usec" in update_usec() instead
v3: recover "now_usec" checks against time jumps within one second, zero it
in update_time() calls instead to pass the check.
--- src/openvpn/otime.h | 1 + 1 file changed, 1 insertion(+)
src/openvpn/otime.h +1 -0
@@ -84,6 +84,7 @@ update_time(void)openvpn_gettimeofday (&tv,NULL );#else update_now (time (NULL )); + now_usec =0 ;#endif }
Signature · 1 line
2.17.1
[Openvpn-devel] [PATCH applied] Re: Fix update_time() and openvpn_gettimeofday() coexistence
I think this is a good first patch for the problem at hand, and it might actually fix one of our (old!) open trac tickets, claiming that the shaper is not (always) working correctly on Linux - Trac#91
For master, we should look more closely into always using "one of the usec providing" time functions, and not mixing time() and gettimeofday() - at least on platforms where this can be done without adverse effects, even if virtualization is involved (making *some* time related functions horribly slow).
I have read the context (otime.h, otime.c), agree with your conclusions, *and* have tested it client-side on Linux and FreeBSD - nothing broke. I have not tested the shaper.
Your patch has been applied to the master and release/2.5 branch.
commit e9e47f498674f6db8c3b88b32c877c5beb09a888 (master) commit 51ee3574faade4dae5762140ea9f3a5eadc9d1e3 (release/2.5)
Author: Vladislav Grishenko Date: Tue Sep 22 22:08:41 2020 +0500
Fix update_time() and openvpn_gettimeofday() coexistence
Signed-off-by: Vladislav Grishenko <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg21070.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering