Skip to contentSkip to conversation

dhcp-server: Fix lease expiry time calculation

PATCH In review Andrew Zaborowski 2 participants 3 messages Jun 4, 2021 – Jun 7, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 dhcp-server: Fix lease expiry time calculation

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.

---
 ell/dhcp-server.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
ell/dhcp-server.c +14 -10
index 50855fe..3d6a6a8 100644
@@ -191,7 +191,6 @@ 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;
 
 	/*
 	 * If this is an expiring lease put it into the expired queue, removing
@@ -214,15 +213,20 @@ 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));
-
-	if (server->next_expire)
-		l_timeout_modify(server->next_expire, next_timeout);
-	else
-		server->next_expire = l_timeout_create(server->lease_seconds,
-							lease_expired_cb,
-							server, NULL);
+	if (server->next_expire) {
+		uint32_t now = l_time_to_secs(l_time_now());
+
+		if (l_time_after(next->lifetime, now)) {
+			unsigned int next_timeout = next->lifetime - now;
+
+			l_timeout_modify(server->next_expire, next_timeout);
+		} else
+			l_timeout_modify_ms(server->next_expire, 1);
+	} else
+		server->next_expire = l_timeout_create(
+						server->lease_seconds,
+						lease_expired_cb,
+						server, NULL);
 }
 
 static void lease_expired_cb(struct l_timeout *timeout, void *user_data)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 dhcp: Track lease lifetime and bind time separately

dhcp-server has been confusing lease->lifetime with the expiry time and also reducing the expiry timestamp resolution and maximum to 32-bit second count. To fix both problems, add microsecond-resultion 64-bit l_dhcp_lease::bound_time (compatible with ell/time.h utils) and use l_dhcp_lease::lifetime exclusively for the life time in seconds. Both fields have the same usage between client and server now. A public getter for the bound time isn't added for now for lack of evidence that it's going to be useful.

---
 ell/dhcp-private.h |  1 +
 ell/dhcp-server.c  | 45 +++++++++++++++++++++++++++++----------------
 ell/dhcp.c         |  2 ++
 3 files changed, 32 insertions(+), 16 deletions(-)
ell/dhcp-private.h +1 -0
index 204e54e..c422322 100644
@@ -158,6 +158,7 @@ struct l_dhcp_lease {
 	uint32_t lifetime;
 	uint32_t t1;
 	uint32_t t2;
+	uint64_t bound_time;
 	uint32_t router;
 	uint32_t *dns;
 	char *domain_name;
ell/dhcp-server.c +29 -16

109 lines not on this page · raw message

ell/dhcp.c +2 -0
index a4afa95..fff1645 100644
@@ -860,6 +860,8 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata)
 
 		dhcp_client_event_notify(client, r);
 
+		client->lease->bound_time = l_time_now();
+
 		/*
 		 * Start T1, once it expires we will start the T2 timer.  If
 		 * we renew the lease, we will end up back here.
Signature · 1 line
2.30.2
#2 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 10 lines · Andrew Zaborowski

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.

---
  ell/dhcp-server.c | 24 ++++++++++++++----------
  1 file changed, 14 insertions(+), 10 deletions(-)

Both applied, thanks.

Regards,
-Denis