Commit 6165df78 authored by Thomas Haller's avatar Thomas Haller

core: move simple utils function from "nm-platform-utils.h"

Most functions defined in "nm-platform-utils.h" perform a lookup
of link properties, for example via ethtool or sysfs. Those functions
depend on the system configuration, such as the current network namespace.

Move the simple helper functions away to "nm-core-internal.h", so that
all remaining functions from "nm-platform-utils.h" are really related to
somthing that interacts with the system/kernel.
parent 0e90f1ba
......@@ -2942,6 +2942,8 @@ nm_utils_parse_debug_string (const char *string,
return result;
}
/*****************************************************************************/
void
nm_utils_ifname_cpy (char *dst, const char *name)
{
......@@ -2954,3 +2956,100 @@ nm_utils_ifname_cpy (char *dst, const char *name)
g_return_if_reached ();
}
/*****************************************************************************/
#define IPV4LL_NETWORK (htonl (0xA9FE0000L))
#define IPV4LL_NETMASK (htonl (0xFFFF0000L))
gboolean
nm_utils_ip4_address_is_link_local (in_addr_t addr)
{
return (addr & IPV4LL_NETMASK) == IPV4LL_NETWORK;
}
/*****************************************************************************/
/**
* 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,
guint32 now,
guint32 padding)
{
gint64 t;
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;
/* Optional padding to avoid potential races. */
t += (gint64) padding;
if (t <= 0)
return 0;
if (t >= NM_PLATFORM_LIFETIME_PERMANENT)
return NM_PLATFORM_LIFETIME_PERMANENT - 1;
return t;
}
gboolean
nm_utils_lifetime_get (guint32 timestamp,
guint32 lifetime,
guint32 preferred,
guint32 now,
guint32 padding,
guint32 *out_lifetime,
guint32 *out_preferred)
{
guint32 t_lifetime, t_preferred;
if (lifetime == 0) {
*out_lifetime = NM_PLATFORM_LIFETIME_PERMANENT;
*out_preferred = NM_PLATFORM_LIFETIME_PERMANENT;
/* We treat lifetime==0 as permanent addresses to allow easy creation of such addresses
* (without requiring to set the lifetime fields to NM_PLATFORM_LIFETIME_PERMANENT).
* In that case we also expect that the other fields (timestamp and preferred) are left unset. */
g_return_val_if_fail (timestamp == 0 && preferred == 0, TRUE);
} else {
if (!now)
now = nm_utils_get_monotonic_timestamp_s ();
t_lifetime = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, padding);
if (!t_lifetime) {
*out_lifetime = 0;
*out_preferred = 0;
return FALSE;
}
t_preferred = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, preferred, now, padding);
*out_lifetime = t_lifetime;
*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), TRUE);
g_return_val_if_fail (t_preferred <= t_lifetime, TRUE);
}
return TRUE;
}
......@@ -30,6 +30,8 @@
/*****************************************************************************/
#define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32
#define NM_DEFINE_SINGLETON_INSTANCE(TYPE) \
static TYPE *singleton_instance
......@@ -414,4 +416,19 @@ guint nm_utils_parse_debug_string (const char *string,
void nm_utils_ifname_cpy (char *dst, const char *name);
guint32 nm_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp,
guint32 duration,
guint32 now,
guint32 padding);
gboolean nm_utils_lifetime_get (guint32 timestamp,
guint32 lifetime,
guint32 preferred,
guint32 now,
guint32 padding,
guint32 *out_lifetime,
guint32 *out_preferred);
gboolean nm_utils_ip4_address_is_link_local (in_addr_t addr);
#endif /* __NM_CORE_UTILS_H__ */
......@@ -188,7 +188,7 @@ routes_are_duplicate (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b,
static gint
_addresses_sort_cmp_get_prio (in_addr_t addr)
{
if (nmp_utils_ip4_address_is_link_local (addr))
if (nm_utils_ip4_address_is_link_local (addr))
return 0;
return 1;
}
......
......@@ -5196,7 +5196,7 @@ ip4_address_add (NMPlatform *platform,
plen,
&peer_addr,
flags,
nmp_utils_ip4_address_is_link_local (addr) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE,
nm_utils_ip4_address_is_link_local (addr) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE,
lifetime,
preferred,
label);
......
......@@ -412,103 +412,6 @@ out:
return g_intern_string (driver);
}
/******************************************************************
* utils
******************************************************************/
#define IPV4LL_NETWORK (htonl (0xA9FE0000L))
#define IPV4LL_NETMASK (htonl (0xFFFF0000L))
gboolean
nmp_utils_ip4_address_is_link_local (in_addr_t addr)
{
return (addr & IPV4LL_NETMASK) == IPV4LL_NETWORK;
}
/**
* 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,
guint32 now,
guint32 padding)
{
gint64 t;
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;
/* Optional padding to avoid potential races. */
t += (gint64) padding;
if (t <= 0)
return 0;
if (t >= NM_PLATFORM_LIFETIME_PERMANENT)
return NM_PLATFORM_LIFETIME_PERMANENT - 1;
return t;
}
gboolean
nmp_utils_lifetime_get (guint32 timestamp,
guint32 lifetime,
guint32 preferred,
guint32 now,
guint32 padding,
guint32 *out_lifetime,
guint32 *out_preferred)
{
guint32 t_lifetime, t_preferred;
if (lifetime == 0) {
*out_lifetime = NM_PLATFORM_LIFETIME_PERMANENT;
*out_preferred = NM_PLATFORM_LIFETIME_PERMANENT;
/* We treat lifetime==0 as permanent addresses to allow easy creation of such addresses
* (without requiring to set the lifetime fields to NM_PLATFORM_LIFETIME_PERMANENT).
* In that case we also expect that the other fields (timestamp and preferred) are left unset. */
g_return_val_if_fail (timestamp == 0 && preferred == 0, TRUE);
} else {
if (!now)
now = nm_utils_get_monotonic_timestamp_s ();
t_lifetime = nmp_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, padding);
if (!t_lifetime) {
*out_lifetime = 0;
*out_preferred = 0;
return FALSE;
}
t_preferred = nmp_utils_lifetime_rebase_relative_time_on_now (timestamp, preferred, now, padding);
*out_lifetime = t_lifetime;
*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), TRUE);
g_return_val_if_fail (t_preferred <= t_lifetime, TRUE);
}
return TRUE;
}
gboolean
nmp_utils_device_exists (const char *name)
{
......
......@@ -52,21 +52,6 @@ gboolean nmp_utils_mii_supports_carrier_detect (const char *ifname);
const char *nmp_utils_udev_get_driver (GUdevDevice *device);
guint32 nmp_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp,
guint32 duration,
guint32 now,
guint32 padding);
gboolean nmp_utils_lifetime_get (guint32 timestamp,
guint32 lifetime,
guint32 preferred,
guint32 now,
guint32 padding,
guint32 *out_lifetime,
guint32 *out_preferred);
gboolean nmp_utils_device_exists (const char *name);
gboolean nmp_utils_ip4_address_is_link_local (in_addr_t addr);
#endif /* __NM_PLATFORM_UTILS_H__ */
......@@ -2588,7 +2588,7 @@ array_contains_ip4_address (const GArray *addresses, const NMPlatformIP4Address
&& ((candidate->peer_address ^ address->peer_address) & nm_utils_ip4_prefix_to_netmask (address->plen)) == 0) {
guint32 lifetime, preferred;
if (nmp_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred,
if (nm_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred,
now, padding, &lifetime, &preferred))
return TRUE;
}
......@@ -2609,7 +2609,7 @@ array_contains_ip6_address (const GArray *addresses, const NMPlatformIP6Address
if (IN6_ARE_ADDR_EQUAL (&candidate->address, &address->address) && candidate->plen == address->plen) {
guint32 lifetime, preferred;
if (nmp_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred,
if (nm_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred,
now, padding, &lifetime, &preferred))
return TRUE;
}
......@@ -2665,7 +2665,7 @@ nm_platform_ip4_address_sync (NMPlatform *self, int ifindex, const GArray *known
const NMPlatformIP4Address *known_address = &g_array_index (known_addresses, NMPlatformIP4Address, i);
guint32 lifetime, preferred;
if (!nmp_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
if (!nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
now, ADDRESS_LIFETIME_PADDING, &lifetime, &preferred))
continue;
......@@ -2727,7 +2727,7 @@ nm_platform_ip6_address_sync (NMPlatform *self, int ifindex, const GArray *known
const NMPlatformIP6Address *known_address = &g_array_index (known_addresses, NMPlatformIP6Address, i);
guint32 lifetime, preferred;
if (!nmp_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
if (!nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
now, ADDRESS_LIFETIME_PADDING, &lifetime, &preferred))
continue;
......@@ -2972,7 +2972,7 @@ _lifetime_to_string (guint32 timestamp, guint32 lifetime, gint32 now, char *buf,
return "forever";
g_snprintf (buf, buf_size, "%usec",
nmp_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, 0));
nm_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, 0));
return buf;
}
......
......@@ -178,8 +178,6 @@ typedef enum {
NM_PLATFORM_SIGNAL_REMOVED,
} NMPlatformSignalChangeType;
#define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32
typedef enum { /*< skip >*/
NM_PLATFORM_GET_ROUTE_FLAGS_NONE = 0,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment