diff options
author | Thomas Haller <thaller@redhat.com> | 2021-03-04 10:05:18 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-03-05 11:27:15 +0100 |
commit | 7b18e15481fcf48d20360dd07a63ac48313225de (patch) | |
tree | 7039830c8f8dec95d5ab86946fb905d11cc55701 | |
parent | 690105c616ed2d684152309d38f9d04fee334144 (diff) | |
download | NetworkManager-7b18e15481fcf48d20360dd07a63ac48313225de.tar.gz |
platform: move nm_utils_lifetime_*() to libnm-platform
-rw-r--r-- | src/core/devices/nm-device.c | 10 | ||||
-rw-r--r-- | src/core/nm-core-utils.c | 83 | ||||
-rw-r--r-- | src/core/nm-core-utils.h | 9 | ||||
-rw-r--r-- | src/core/platform/nm-platform.c | 14 | ||||
-rw-r--r-- | src/libnm-platform/nm-platform-utils.c | 86 | ||||
-rw-r--r-- | src/libnm-platform/nm-platform-utils.h | 9 |
6 files changed, 107 insertions, 104 deletions
diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index aa2f0ab704..9ee4d2c93d 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -5216,11 +5216,11 @@ ndisc_set_router_config(NMNDisc *ndisc, NMDevice *self) if (addr->plen != 64) continue; - lifetime = nm_utils_lifetime_get(addr->timestamp, - addr->lifetime, - addr->preferred, - NM_NDISC_EXPIRY_BASE_TIMESTAMP / 1000, - &preferred); + lifetime = nmp_utils_lifetime_get(addr->timestamp, + addr->lifetime, + addr->preferred, + NM_NDISC_EXPIRY_BASE_TIMESTAMP / 1000, + &preferred); if (!lifetime) continue; diff --git a/src/core/nm-core-utils.c b/src/core/nm-core-utils.c index 48e90f2dc7..7e5bd20afe 100644 --- a/src/core/nm-core-utils.c +++ b/src/core/nm-core-utils.c @@ -3969,89 +3969,6 @@ nm_utils_g_value_set_strv(GValue *value, GPtrArray *strings) /*****************************************************************************/ -/** - * Takes a pair @timestamp and @duration, and returns the remaining duration based - * on the new timestamp @now. - */ -guint32 -nm_utils_lifetime_rebase_relative_time_on_now(guint32 timestamp, guint32 duration, gint32 now) -{ - gint64 t; - - nm_assert(now >= 0); - - if (duration == NM_PLATFORM_LIFETIME_PERMANENT) - return NM_PLATFORM_LIFETIME_PERMANENT; - - if (timestamp == 0) { - /* if the @timestamp is zero, assume it was just left unset and that the relative - * @duration starts counting from @now. This is convenient to construct an address - * and print it in nm_platform_ip4_address_to_string(). - * - * In general it does not make sense to set the @duration without anchoring at - * @timestamp because you don't know the absolute expiration time when looking - * at the address at a later moment. */ - timestamp = now; - } - - /* For timestamp > now, just accept it and calculate the expected(?) result. */ - t = (gint64) timestamp + (gint64) duration - (gint64) now; - - if (t <= 0) - return 0; - if (t >= NM_PLATFORM_LIFETIME_PERMANENT) - return NM_PLATFORM_LIFETIME_PERMANENT - 1; - return t; -} - -guint32 -nm_utils_lifetime_get(guint32 timestamp, - guint32 lifetime, - guint32 preferred, - gint32 now, - guint32 *out_preferred) -{ - guint32 t_lifetime, t_preferred; - - nm_assert(now >= 0); - - if (timestamp == 0 && lifetime == 0) { - /* We treat lifetime==0 && timestamp==0 addresses as permanent addresses to allow easy - * creation of such addresses (without requiring to set the lifetime fields to - * NM_PLATFORM_LIFETIME_PERMANENT). The real lifetime==0 addresses (E.g. DHCP6 telling us - * to drop an address will have timestamp set. - */ - NM_SET_OUT(out_preferred, NM_PLATFORM_LIFETIME_PERMANENT); - g_return_val_if_fail(preferred == 0, NM_PLATFORM_LIFETIME_PERMANENT); - return NM_PLATFORM_LIFETIME_PERMANENT; - } - - if (now <= 0) - now = nm_utils_get_monotonic_timestamp_sec(); - - t_lifetime = nm_utils_lifetime_rebase_relative_time_on_now(timestamp, lifetime, now); - if (!t_lifetime) { - NM_SET_OUT(out_preferred, 0); - return 0; - } - - t_preferred = nm_utils_lifetime_rebase_relative_time_on_now(timestamp, preferred, now); - - NM_SET_OUT(out_preferred, MIN(t_preferred, t_lifetime)); - - /* Assert that non-permanent addresses have a (positive) @timestamp. nm_utils_lifetime_rebase_relative_time_on_now() - * treats addresses with timestamp 0 as *now*. Addresses passed to _address_get_lifetime() always - * should have a valid @timestamp, otherwise on every re-sync, their lifetime will be extended anew. - */ - g_return_val_if_fail(timestamp != 0 - || (lifetime == NM_PLATFORM_LIFETIME_PERMANENT - && preferred == NM_PLATFORM_LIFETIME_PERMANENT), - t_lifetime); - g_return_val_if_fail(t_preferred <= t_lifetime, t_lifetime); - - return t_lifetime; -} - const char * nm_utils_dnsmasq_status_to_string(int status, char *dest, gsize size) { diff --git a/src/core/nm-core-utils.h b/src/core/nm-core-utils.h index 23d0382181..cf328ffc81 100644 --- a/src/core/nm-core-utils.h +++ b/src/core/nm-core-utils.h @@ -371,15 +371,6 @@ void _nm_utils_set_testing(NMUtilsTestFlags flags); void nm_utils_g_value_set_strv(GValue *value, GPtrArray *strings); -guint32 -nm_utils_lifetime_rebase_relative_time_on_now(guint32 timestamp, guint32 duration, gint32 now); - -guint32 nm_utils_lifetime_get(guint32 timestamp, - guint32 lifetime, - guint32 preferred, - gint32 now, - guint32 *out_preferred); - /*****************************************************************************/ const char *nm_utils_dnsmasq_status_to_string(int status, char *dest, gsize size); diff --git a/src/core/platform/nm-platform.c b/src/core/platform/nm-platform.c index af502fa8db..5875217938 100644 --- a/src/core/platform/nm-platform.c +++ b/src/core/platform/nm-platform.c @@ -3737,7 +3737,7 @@ _addr_array_clean_expired(int addr_family, goto clear_and_next; } - if (!nm_utils_lifetime_get(a->timestamp, a->lifetime, a->preferred, now, NULL)) + if (!nmp_utils_lifetime_get(a->timestamp, a->lifetime, a->preferred, now, NULL)) goto clear_and_next; if (idx) { @@ -4222,11 +4222,11 @@ next_plat:; known_address = NMP_OBJECT_CAST_IPX_ADDRESS(o); - lifetime = nm_utils_lifetime_get(known_address->ax.timestamp, - known_address->ax.lifetime, - known_address->ax.preferred, - now, - &preferred); + lifetime = nmp_utils_lifetime_get(known_address->ax.timestamp, + known_address->ax.lifetime, + known_address->ax.preferred, + now, + &preferred); nm_assert(lifetime > 0); if (IS_IPv4) { @@ -5421,7 +5421,7 @@ _lifetime_to_string(guint32 timestamp, guint32 lifetime, gint32 now, char *buf, g_snprintf(buf, buf_size, "%usec", - nm_utils_lifetime_rebase_relative_time_on_now(timestamp, lifetime, now)); + nmp_utils_lifetime_rebase_relative_time_on_now(timestamp, lifetime, now)); return buf; } diff --git a/src/libnm-platform/nm-platform-utils.c b/src/libnm-platform/nm-platform-utils.c index 145f6f0a03..b81abd142e 100644 --- a/src/libnm-platform/nm-platform-utils.c +++ b/src/libnm-platform/nm-platform-utils.c @@ -20,6 +20,7 @@ #include "libnm-base/nm-ethtool-base.h" #include "libnm-log-core/nm-logging.h" +#include "libnm-glib-aux/nm-time-utils.h" /*****************************************************************************/ @@ -1866,3 +1867,88 @@ nmp_utils_new_infiniband_name(char *name, const char *parent_name, int p_key) g_snprintf(name, IFNAMSIZ, "%s.%04x", parent_name, p_key); return name; } + +/*****************************************************************************/ + +/** + * Takes a pair @timestamp and @duration, and returns the remaining duration based + * on the new timestamp @now. + */ +guint32 +nmp_utils_lifetime_rebase_relative_time_on_now(guint32 timestamp, guint32 duration, gint32 now) +{ + gint64 t; + + nm_assert(now >= 0); + + if (duration == NM_PLATFORM_LIFETIME_PERMANENT) + return NM_PLATFORM_LIFETIME_PERMANENT; + + if (timestamp == 0) { + /* if the @timestamp is zero, assume it was just left unset and that the relative + * @duration starts counting from @now. This is convenient to construct an address + * and print it in nm_platform_ip4_address_to_string(). + * + * In general it does not make sense to set the @duration without anchoring at + * @timestamp because you don't know the absolute expiration time when looking + * at the address at a later moment. */ + timestamp = now; + } + + /* For timestamp > now, just accept it and calculate the expected(?) result. */ + t = (gint64) timestamp + (gint64) duration - (gint64) now; + + if (t <= 0) + return 0; + if (t >= NM_PLATFORM_LIFETIME_PERMANENT) + return NM_PLATFORM_LIFETIME_PERMANENT - 1; + return t; +} + +guint32 +nmp_utils_lifetime_get(guint32 timestamp, + guint32 lifetime, + guint32 preferred, + gint32 now, + guint32 *out_preferred) +{ + guint32 t_lifetime, t_preferred; + + nm_assert(now >= 0); + + if (timestamp == 0 && lifetime == 0) { + /* We treat lifetime==0 && timestamp==0 addresses as permanent addresses to allow easy + * creation of such addresses (without requiring to set the lifetime fields to + * NM_PLATFORM_LIFETIME_PERMANENT). The real lifetime==0 addresses (E.g. DHCP6 telling us + * to drop an address will have timestamp set. + */ + NM_SET_OUT(out_preferred, NM_PLATFORM_LIFETIME_PERMANENT); + g_return_val_if_fail(preferred == 0, NM_PLATFORM_LIFETIME_PERMANENT); + return NM_PLATFORM_LIFETIME_PERMANENT; + } + + if (now <= 0) + now = nm_utils_get_monotonic_timestamp_sec(); + + t_lifetime = nmp_utils_lifetime_rebase_relative_time_on_now(timestamp, lifetime, now); + if (!t_lifetime) { + NM_SET_OUT(out_preferred, 0); + return 0; + } + + t_preferred = nmp_utils_lifetime_rebase_relative_time_on_now(timestamp, preferred, now); + + NM_SET_OUT(out_preferred, MIN(t_preferred, t_lifetime)); + + /* Assert that non-permanent addresses have a (positive) @timestamp. nmp_utils_lifetime_rebase_relative_time_on_now() + * treats addresses with timestamp 0 as *now*. Addresses passed to _address_get_lifetime() always + * should have a valid @timestamp, otherwise on every re-sync, their lifetime will be extended anew. + */ + g_return_val_if_fail(timestamp != 0 + || (lifetime == NM_PLATFORM_LIFETIME_PERMANENT + && preferred == NM_PLATFORM_LIFETIME_PERMANENT), + t_lifetime); + g_return_val_if_fail(t_preferred <= t_lifetime, t_lifetime); + + return t_lifetime; +} diff --git a/src/libnm-platform/nm-platform-utils.h b/src/libnm-platform/nm-platform-utils.h index 5ebcd4d9d2..898d0f6fbc 100644 --- a/src/libnm-platform/nm-platform-utils.h +++ b/src/libnm-platform/nm-platform-utils.h @@ -73,4 +73,13 @@ int nmp_utils_sysctl_open_netdir(int ifindex, const char *ifname_guess, char *ou char * nmp_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id); const char *nmp_utils_new_infiniband_name(char *name, const char *parent_name, int p_key); +guint32 +nmp_utils_lifetime_rebase_relative_time_on_now(guint32 timestamp, guint32 duration, gint32 now); + +guint32 nmp_utils_lifetime_get(guint32 timestamp, + guint32 lifetime, + guint32 preferred, + gint32 now, + guint32 *out_preferred); + #endif /* __NM_PLATFORM_UTILS_H__ */ |