From patchwork Thu Oct 6 01:29:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 2803 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director15.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id WLhGK3PKPmOaCgAAIUCqbw (envelope-from ) for ; Thu, 06 Oct 2022 08:30:43 -0400 Received: from proxy15.mail.ord1d.rsapps.net ([172.30.191.6]) by director15.mail.ord1d.rsapps.net with LMTP id uKMyK3PKPmM3YwAAIcMcQg (envelope-from ) for ; Thu, 06 Oct 2022 08:30:43 -0400 Received: from smtp40.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy15.mail.ord1d.rsapps.net with LMTPS id IPXxKnPKPmOaHgAAAY1PeQ (envelope-from ) for ; Thu, 06 Oct 2022 08:30:43 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp40.gate.ord1d.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=rfc2549.org X-Suspicious-Flag: YES X-Classification-ID: b15b831e-4572-11ed-95a9-525400f204c2-1-1 Received: from [216.105.38.7] ([216.105.38.7:37394] helo=lists.sourceforge.net) by smtp40.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 75/DD-22876-27ACE336; Thu, 06 Oct 2022 08:30:42 -0400 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1ogQ0e-0006uC-07; Thu, 06 Oct 2022 12:29:52 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1ogQ0a-0006tp-Qd for openvpn-devel@lists.sourceforge.net; Thu, 06 Oct 2022 12:29:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=X0rqs8sSIRiwJYjjgHkIdkIiY/7Zxfc6I/0gzxkL3n0=; b=lRMgBad4lYN07KBF6JUIRadI6s XjPW7i3OIGtIHbPsTEeMeRJzuew4/MfxvD2b5nQD+mVDeumos+L0sFlKD/+jm3asGLSS8N2BWEVfJ /2+IKi2mU/9jG9S4G+XdGFy1bFLlv7dyELbwvww7LmZEgyHfCIprPs9Tpb7Owx3X/jyw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=X0rqs8sSIRiwJYjjgHkIdkIiY/7Zxfc6I/0gzxkL3n0=; b=V5iXj7pTOtrAGIurGweINNhqM8 /vUEFJPSaAqIVjNUsNMLOnf38y3e8xK4kXP7OYG4UXuIE3n9Xz/I8ar2M092gWHDDeguEG0bShLLq Jk9OJc1eilVw1irvi39m1QSwQkVK6hLjc/FFpVxVKNNrgyhYl0csl/Wn8PJfyZ8PglVg=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1ogQ0Z-00Egkl-I1 for openvpn-devel@lists.sourceforge.net; Thu, 06 Oct 2022 12:29:48 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.95 (FreeBSD)) (envelope-from ) id 1ogQ0S-000E7e-4b for openvpn-devel@lists.sourceforge.net; Thu, 06 Oct 2022 14:29:40 +0200 Received: (nullmailer pid 1202758 invoked by uid 10006); Thu, 06 Oct 2022 12:29:40 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 6 Oct 2022 14:29:40 +0200 Message-Id: <20221006122940.1202712-1-arne@rfc2549.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220923085804.3743109-1-arne@rfc2549.org> References: <20220923085804.3743109-1-arne@rfc2549.org> MIME-Version: 1.0 X-Spam-Report: Spam detection software, running on the system "util-spamd-2.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Remove function event_timeout_clear_ret as it is unused. Cleanup event_timeout_trigger a bit. Do an instant return false if the timeout is not defined and inline local_now and use event_timeout_remaining instead of local duplicated code. Content analysis details: (0.3 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-Headers-End: 1ogQ0Z-00Egkl-I1 Subject: [Openvpn-devel] [PATCH v2] Document/cleanup event_timeout functions X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox Remove function event_timeout_clear_ret as it is unused. Cleanup event_timeout_trigger a bit. Do an instant return false if the timeout is not defined and inline local_now and use event_timeout_remaining instead of local duplicated code. Add doxygen comments for all timeout function, especially for the event_timeout_trigger function that is hard to understand otherwise. Patch v2: add many fixes/correction suggested by Frank Signed-off-by: Arne Schwabe Acked-By: Frank Lichtenheld --- src/openvpn/interval.c | 50 ++++++++++---------- src/openvpn/interval.h | 103 +++++++++++++++++++++++++++++------------ src/openvpn/openvpn.h | 4 +- 3 files changed, 102 insertions(+), 55 deletions(-) diff --git a/src/openvpn/interval.c b/src/openvpn/interval.c index 2f0fc42dd..8f3c1c604 100644 --- a/src/openvpn/interval.c +++ b/src/openvpn/interval.c @@ -41,44 +41,46 @@ interval_init(struct interval *top, int horizon, int refresh) top->horizon = horizon; } + bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry) { + if (!et->defined) + { + return false; + } + bool ret = false; - const time_t local_now = now; + time_t wakeup = event_timeout_remaining(et); - if (et->defined) + if (wakeup <= 0) { - time_t wakeup = et->last - local_now + et->n; - if (wakeup <= 0) - { #if INTERVAL_DEBUG - dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n, - et_const_retry); + dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n, + et_const_retry); #endif - if (et_const_retry < 0) - { - et->last = local_now; - wakeup = et->n; - ret = true; - } - else - { - wakeup = et_const_retry; - } + if (et_const_retry < 0) + { + et->last = now; + wakeup = et->n; + ret = true; } - - if (tv && wakeup < tv->tv_sec) + else { + wakeup = et_const_retry; + } + } + + if (tv && wakeup < tv->tv_sec) + { #if INTERVAL_DEBUG - dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d", - (int) wakeup, et->n, et_const_retry); + dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d", + (int) wakeup, et->n, et_const_retry); #endif - tv->tv_sec = wakeup; - tv->tv_usec = 0; - } + tv->tv_sec = wakeup; + tv->tv_usec = 0; } return ret; } diff --git a/src/openvpn/interval.h b/src/openvpn/interval.h index f58bfacf6..e6f0bece1 100644 --- a/src/openvpn/interval.h +++ b/src/openvpn/interval.h @@ -135,9 +135,9 @@ interval_action(struct interval *top) struct event_timeout { - bool defined; - interval_t n; - time_t last; /* time of last event */ + bool defined; /**< This timeout is active */ + interval_t n; /**< periodic interval for periodic timeouts */ + time_t last; /**< time of last event */ }; static inline bool @@ -145,7 +145,12 @@ event_timeout_defined(const struct event_timeout *et) { return et->defined; } - +/** + * Clears the timeout and reset all values to 0. Following timer checks will + * not trigger. + * + * @param et timer struct + */ static inline void event_timeout_clear(struct event_timeout *et) { @@ -154,22 +159,32 @@ event_timeout_clear(struct event_timeout *et) et->last = 0; } -static inline struct event_timeout -event_timeout_clear_ret(void) -{ - struct event_timeout ret; - event_timeout_clear(&ret); - return ret; -} +/** + * Initialises a timer struct. The timer will become true/trigger after + * last + n seconds. + * + * + * @param et Timer struct + * @param n Interval of the timer for periodic timer. A negative + * value for n will be interpreted as 0. + * @param last Sets the base time of the timer. + */ static inline void -event_timeout_init(struct event_timeout *et, interval_t n, const time_t local_now) +event_timeout_init(struct event_timeout *et, interval_t n, const time_t last) { et->defined = true; et->n = (n >= 0) ? n : 0; - et->last = local_now; + et->last = last; } +/** + * Resets a timer. + * + * Sets the last time the timer has been triggered for the calculation of the + * next event. + * @param et + */ static inline void event_timeout_reset(struct event_timeout *et) { @@ -179,42 +194,70 @@ event_timeout_reset(struct event_timeout *et) } } +/** + * Sets the interval n of a timeout. + * @param et + * @param n Interval value of the timer, negative values + * will be interpreted as 0 + * + * @note you might need to call reset_coarse_timers after this + */ static inline void event_timeout_modify_wakeup(struct event_timeout *et, interval_t n) { - /* note that you might need to call reset_coarse_timers after this */ if (et->defined) { et->n = (n >= 0) ? n : 0; } } -/* - * Will return the time left for a timeout, this function does not check - * if the timeout is actually valid +/** + * Returns the time until the timeout should triggered from from now. + * This function does not check if the timeout is actually valid. */ static inline interval_t event_timeout_remaining(struct event_timeout *et) { - return (interval_t) (et->last - now + et->n); + return (interval_t) ((et->last + et->n) - now); } -/* - * This is the principal function for testing and triggering recurring - * timers and will return true on a timer signal event. - * If et_const_retry == ETT_DEFAULT and a signal occurs, - * the function will return true and *et will be armed for the - * next event. If et_const_retry >= 0 and a signal occurs, - * *et will not be touched, but *tv will be set to - * minimum (*tv, et_const_retry) for a future re-test, - * and the function will return true. - */ - #define ETT_DEFAULT (-1) +/** + * This is the principal function for testing and triggering recurring + * timers. + * + * If *et is not triggered, *tv is set to remaining time until the timeout if + * not already lower: + * + * *tv = minimum(*tv, event_timeout_remaining(*et)) + * + * If *et triggers and et_const_retry is negative (ETT_DEFAULT is -1): + * - the function will return true + * - *et will be armed for the next event (et->last set to now). + * - *tv will be lowered to the event period (n) if larger than the + * period of the event (set to *et's next timeout) + * - *et will not changed (ie also not rearmed, stays armed) + * + * If *et triggers and et_const_retry >= 0, *tv will be lowered to et_const_try + * if larger: + * + * *tv = *minimum(*tv, et_const_retry) + * + * This is mainly useful is the trigger cannot yet be triggered for other + * reasons and a backoff timeout should be returned if the timer is ready + * to trigger. + * + * + * @param et the timeout to check + * @param tv will be set to timeout for next check for this + * timeout unless already smaller. + * @param et_const_retry see above + * @return if the timeout has triggered and event has been reset + */ bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, - const int et_const_retry); + int et_const_retry); /* * Measure time intervals in microseconds diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h index 00cd652fa..95c7d7520 100644 --- a/src/openvpn/openvpn.h +++ b/src/openvpn/openvpn.h @@ -386,7 +386,9 @@ struct context_2 * Event loop info */ - /* how long to wait on link/tun read before we will need to be serviced */ + /** Time to next event of timers and similar. This is used to determine + * how long to wait on event wait (select/poll on link/tun read) + * before this context wants to be serviced */ struct timeval timeval; /* next wakeup for processing coarse timers (>1 sec resolution) */