All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Zaborowski <andrew.zaborowski@intel.com>
To: ell@lists.01.org
Subject: [PATCH] dhcp-server: Fix lease expiry time calculation
Date: Wed, 02 Jun 2021 01:53:32 +0200	[thread overview]
Message-ID: <20210601235332.95056-1-andrew.zaborowski@intel.com> (raw)

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

We were comparing the l_time_now() (usecs) to lease->lifetime (secs)
and then converting the result from usecs to secs, so the "diff" and the
"to_secs" operations need to be switched around.  While there, use the
opportunity to replace the "diff" operation so that if lease->lifetime is
already in the past (e.g. because processing took to long), we schedule
the timeout in 1 millisec instead of the absolute value of the difference.

Since we're using uint32_t for the timestamps, use the overflow-safe(r)
comparisons in set_next_expire_timer and in is_expired_lease.
---
 ell/dhcp-server.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/ell/dhcp-server.c b/ell/dhcp-server.c
index ebd4438..38193a2 100644
--- a/ell/dhcp-server.c
+++ b/ell/dhcp-server.c
@@ -104,7 +104,7 @@ struct l_dhcp_server {
 
 static bool is_expired_lease(struct l_dhcp_lease *lease)
 {
-	if (lease->lifetime < l_time_to_secs(l_time_now()))
+	if ((int32_t) (lease->lifetime - l_time_to_secs(l_time_now())) < 0)
 		return true;
 
 	return false;
@@ -191,7 +191,8 @@ static void set_next_expire_timer(struct l_dhcp_server *server,
 					struct l_dhcp_lease *expired)
 {
 	struct l_dhcp_lease *next;
-	unsigned int next_timeout;
+	unsigned long next_timeout;
+	uint32_t now;
 
 	/*
 	 * If this is an expiring lease put it into the expired queue, removing
@@ -214,11 +215,12 @@ static void set_next_expire_timer(struct l_dhcp_server *server,
 		return;
 	}
 
-	next_timeout = l_time_to_secs(l_time_diff(l_time_now(),
-							next->lifetime));
+	now = l_time_to_secs(l_time_now());
+	next_timeout = (int32_t) (next->lifetime - now) > 0 ?
+		(next->lifetime - now) * 1000L : 1;
 
 	if (server->next_expire)
-		l_timeout_modify(server->next_expire, next_timeout);
+		l_timeout_modify_ms(server->next_expire, next_timeout);
 	else
 		server->next_expire = l_timeout_create(server->lease_seconds,
 							lease_expired_cb,
-- 
2.30.2

             reply	other threads:[~2021-06-01 23:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-01 23:53 Andrew Zaborowski [this message]
2021-06-04 16:49 ` [PATCH] dhcp-server: Fix lease expiry time calculation Denis Kenzior
2021-06-04 17:35   ` Andrew Zaborowski
2021-06-04 18:00     ` Denis Kenzior
2021-06-04 18:34       ` Andrew Zaborowski
2021-06-04 18:48         ` Denis Kenzior

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=20210601235332.95056-1-andrew.zaborowski@intel.com \
    --to=andrew.zaborowski@intel.com \
    --cc=ell@lists.01.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.