1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-29 06:50:16 +03:00

Merge pull request #29092 from yuwata/sd-dhcp-server-use-usec

sd-dhcp-server: store lifetime and friends in usec_t
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2023-09-20 11:43:26 +02:00 committed by GitHub
commit 0d05d1623e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 108 additions and 36 deletions

View File

@ -81,7 +81,8 @@ struct sd_dhcp_server {
Hashmap *static_leases_by_client_id;
Hashmap *static_leases_by_address;
uint32_t max_lease_time, default_lease_time;
usec_t max_lease_time;
usec_t default_lease_time;
sd_dhcp_server_callback_t callback;
void *callback_userdata;
@ -101,7 +102,7 @@ typedef struct DHCPRequest {
size_t max_optlen;
be32_t server_id;
be32_t requested_ip;
uint32_t lifetime;
usec_t lifetime;
const uint8_t *agent_info_option;
char *hostname;
} DHCPRequest;

View File

@ -2,6 +2,7 @@
#include "format-util.h"
#include "network-common.h"
#include "unaligned.h"
int get_ifname(int ifindex, char **ifname) {
assert(ifname);
@ -13,3 +14,82 @@ int get_ifname(int ifindex, char **ifname) {
return format_ifname_alloc(ifindex, ifname);
}
usec_t unaligned_be32_sec_to_usec(const void *p, bool max_as_infinity) {
uint32_t s = unaligned_read_be32(ASSERT_PTR(p));
if (s == UINT32_MAX && max_as_infinity)
return USEC_INFINITY;
return s * USEC_PER_SEC;
}
usec_t be32_sec_to_usec(be32_t t, bool max_as_infinity) {
uint32_t s = be32toh(t);
if (s == UINT32_MAX && max_as_infinity)
return USEC_INFINITY;
return s * USEC_PER_SEC;
}
usec_t be32_msec_to_usec(be32_t t, bool max_as_infinity) {
uint32_t s = be32toh(t);
if (s == UINT32_MAX && max_as_infinity)
return USEC_INFINITY;
return s * USEC_PER_MSEC;
}
usec_t be16_sec_to_usec(be16_t t, bool max_as_infinity) {
uint16_t s = be16toh(t);
if (s == UINT16_MAX && max_as_infinity)
return USEC_INFINITY;
return s * USEC_PER_SEC;
}
be32_t usec_to_be32_sec(usec_t t) {
if (t == USEC_INFINITY)
/* Some settings, e.g. a lifetime of an address, UINT32_MAX is handled as infinity. so let's
* map USEC_INFINITY to UINT32_MAX. */
return htobe32(UINT32_MAX);
if (t >= (UINT32_MAX - 1) * USEC_PER_SEC)
/* Finite but too large. Let's use the largest (or off-by-one from the largest) finite value. */
return htobe32(UINT32_MAX - 1);
return htobe32((uint32_t) DIV_ROUND_UP(t, USEC_PER_SEC));
}
be32_t usec_to_be32_msec(usec_t t) {
if (t == USEC_INFINITY)
return htobe32(UINT32_MAX);
if (t >= (UINT32_MAX - 1) * USEC_PER_MSEC)
return htobe32(UINT32_MAX - 1);
return htobe32((uint32_t) DIV_ROUND_UP(t, USEC_PER_MSEC));
}
be16_t usec_to_be16_sec(usec_t t) {
if (t == USEC_INFINITY)
return htobe16(UINT16_MAX);
if (t >= (UINT16_MAX - 1) * USEC_PER_SEC)
return htobe16(UINT16_MAX - 1);
return htobe16((uint16_t) DIV_ROUND_UP(t, USEC_PER_SEC));
}
usec_t time_span_to_stamp(usec_t span, usec_t base) {
/* Typically, 0 lifetime (timespan) indicates the corresponding configuration (address or so) must be
* dropped. So, when the timespan is zero, here we return 0 rather than 'base'. This makes the caller
* easily understand that the configuration needs to be dropped immediately. */
if (span == 0)
return 0;
return usec_add(base, span);
}

View File

@ -2,6 +2,8 @@
#pragma once
#include "log-link.h"
#include "sparse-endian.h"
#include "time-util.h"
#define log_interface_prefix_full_errno_zerook(prefix, type, val, error, fmt, ...) \
({ \
@ -28,3 +30,12 @@
})
int get_ifname(int ifindex, char **ifname);
usec_t unaligned_be32_sec_to_usec(const void *p, bool max_as_infinity);
usec_t be32_sec_to_usec(be32_t t, bool max_as_infinity);
usec_t be32_msec_to_usec(be32_t t, bool max_as_infinity);
usec_t be16_sec_to_usec(be16_t t, bool max_as_infinity);
be32_t usec_to_be32_sec(usec_t t);
be32_t usec_to_be32_msec(usec_t t);
be16_t usec_to_be16_sec(usec_t t);
usec_t time_span_to_stamp(usec_t span, usec_t base);

View File

@ -206,8 +206,8 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
.netmask = htobe32(INADDR_ANY),
.ifindex = ifindex,
.bind_to_interface = true,
.default_lease_time = DIV_ROUND_UP(DHCP_DEFAULT_LEASE_TIME_USEC, USEC_PER_SEC),
.max_lease_time = DIV_ROUND_UP(DHCP_MAX_LEASE_TIME_USEC, USEC_PER_SEC),
.default_lease_time = DHCP_DEFAULT_LEASE_TIME_USEC,
.max_lease_time = DHCP_MAX_LEASE_TIME_USEC,
};
*ret = TAKE_PTR(server);
@ -589,7 +589,7 @@ static int server_send_offer_or_ack(
packet->dhcp.yiaddr = address;
packet->dhcp.siaddr = server->boot_server_address.s_addr;
lease_time = htobe32(req->lifetime);
lease_time = usec_to_be32_sec(req->lifetime);
r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME, 4,
&lease_time);
@ -735,7 +735,7 @@ static int parse_request(uint8_t code, uint8_t len, const void *option, void *us
switch (code) {
case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
if (len == 4)
req->lifetime = unaligned_read_be32(option);
req->lifetime = unaligned_be32_sec_to_usec(option, /* max_as_infinity = */ true);
break;
case SD_DHCP_OPTION_REQUESTED_IP_ADDRESS:
@ -844,7 +844,7 @@ static int ensure_sane_request(sd_dhcp_server *server, DHCPRequest *req, DHCPMes
req->max_optlen = DHCP_MIN_OPTIONS_SIZE;
if (req->lifetime <= 0)
req->lifetime = MAX(1ULL, server->default_lease_time);
req->lifetime = MAX(USEC_PER_SEC, server->default_lease_time);
if (server->max_lease_time > 0 && req->lifetime > server->max_lease_time)
req->lifetime = server->max_lease_time;
@ -997,7 +997,7 @@ static int server_ack_request(sd_dhcp_server *server, DHCPRequest *req, DHCPLeas
if (r < 0)
return r;
expiration = usec_add(req->lifetime * USEC_PER_SEC, time_now);
expiration = usec_add(req->lifetime, time_now);
if (existing_lease) {
assert(existing_lease->server);
@ -1487,24 +1487,18 @@ int sd_dhcp_server_set_timezone(sd_dhcp_server *server, const char *tz) {
return 1;
}
int sd_dhcp_server_set_max_lease_time(sd_dhcp_server *server, uint32_t t) {
int sd_dhcp_server_set_max_lease_time(sd_dhcp_server *server, uint64_t t) {
assert_return(server, -EINVAL);
if (t == server->max_lease_time)
return 0;
server->max_lease_time = t;
return 1;
return 0;
}
int sd_dhcp_server_set_default_lease_time(sd_dhcp_server *server, uint32_t t) {
int sd_dhcp_server_set_default_lease_time(sd_dhcp_server *server, uint64_t t) {
assert_return(server, -EINVAL);
if (t == server->default_lease_time)
return 0;
server->default_lease_time = t;
return 1;
return 0;
}
int sd_dhcp_server_set_servers(

View File

@ -126,18 +126,6 @@ static bool router_lifetime_is_valid(usec_t lifetime_usec) {
lifetime_usec <= RADV_MAX_ROUTER_LIFETIME_USEC);
}
static be32_t usec_to_be32_sec(usec_t usec) {
if (usec == USEC_INFINITY)
/* UINT32_MAX is handled as infinity. */
return htobe32(UINT32_MAX);
if (usec >= UINT32_MAX * USEC_PER_SEC)
/* Finite but too large. Let's use the largest finite value. */
return htobe32(UINT32_MAX - 1);
return htobe32(usec / USEC_PER_SEC);
}
static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_usec) {
struct sockaddr_in6 dst_addr = {
.sin6_family = AF_INET6,

View File

@ -391,15 +391,13 @@ static int dhcp4_server_configure(Link *link) {
return log_link_error_errno(link, r, "Failed to configure address pool for DHCPv4 server instance: %m");
if (link->network->dhcp_server_max_lease_time_usec > 0) {
r = sd_dhcp_server_set_max_lease_time(link->dhcp_server,
DIV_ROUND_UP(link->network->dhcp_server_max_lease_time_usec, USEC_PER_SEC));
r = sd_dhcp_server_set_max_lease_time(link->dhcp_server, link->network->dhcp_server_max_lease_time_usec);
if (r < 0)
return log_link_error_errno(link, r, "Failed to set maximum lease time for DHCPv4 server instance: %m");
}
if (link->network->dhcp_server_default_lease_time_usec > 0) {
r = sd_dhcp_server_set_default_lease_time(link->dhcp_server,
DIV_ROUND_UP(link->network->dhcp_server_default_lease_time_usec, USEC_PER_SEC));
r = sd_dhcp_server_set_default_lease_time(link->dhcp_server, link->network->dhcp_server_default_lease_time_usec);
if (r < 0)
return log_link_error_errno(link, r, "Failed to set default lease time for DHCPv4 server instance: %m");
}

View File

@ -82,8 +82,8 @@ int sd_dhcp_server_add_option(sd_dhcp_server *server, sd_dhcp_option *v);
int sd_dhcp_server_add_vendor_option(sd_dhcp_server *server, sd_dhcp_option *v);
int sd_dhcp_server_set_static_lease(sd_dhcp_server *server, const struct in_addr *address, uint8_t *client_id, size_t client_id_size);
int sd_dhcp_server_set_max_lease_time(sd_dhcp_server *server, uint32_t t);
int sd_dhcp_server_set_default_lease_time(sd_dhcp_server *server, uint32_t t);
int sd_dhcp_server_set_max_lease_time(sd_dhcp_server *server, uint64_t t);
int sd_dhcp_server_set_default_lease_time(sd_dhcp_server *server, uint64_t t);
int sd_dhcp_server_forcerenew(sd_dhcp_server *server);