diff options
author | Thomas Haller <thaller@redhat.com> | 2015-06-20 12:05:01 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-06-21 12:53:48 +0200 |
commit | e8e455817b340f60b396ba5d41425ed4de4c8554 (patch) | |
tree | eabf5d6dfcf2ac2f35d92e7f6069f6d98f562d65 | |
parent | ae824f582b0dbb9fe49e1aa7fb1d27319028892b (diff) | |
download | NetworkManager-e8e455817b340f60b396ba5d41425ed4de4c8554.tar.gz |
platform: refactor virtual methods for link objects in NMPlatform
Change nm_platform_link_get() to return the cached NMPlatformLink
instance. Now what all our implementations (fake and linux) have such a
cache internal object, let's just expose it directly.
Note that the lifetime of the exposed link object is possibly quite
short. A caller must copy the returned value if he intends to preserve
it for later.
Also add nm_platform_link_get_by_ifname() and modify nm_platform_link_get_by_address()
to return the instance.
Certain functions, such as nm_platform_link_get_name(),
nm_platform_link_get_ifindex(), etc. are solely implemented based
on looking at the returned NMPlatformLink object. No longer implement
them as virtual functions but instead implement them in the base class
(nm-platform.c).
This removes code and eliminates the redundancy of the exposed
NMPlatformLink instance and the nm_platform_link_get_*() accessors.
Thereby also fix a bug in NMFakePlatform that tracked the link address
in a separate "address" field, instead of using "link.addr". That was
a case where the redundancy actually led to a bug in fake platform.
Also remove some stub implementations in NMFakePlatform that just
bail out. Instead allow for a missing virtual functions and perform
the "default" action in the accessor.
An example for that is nm_platform_link_get_permanent_address().
-rw-r--r-- | src/NetworkManagerUtils.c | 2 | ||||
-rw-r--r-- | src/devices/nm-device.c | 17 | ||||
-rw-r--r-- | src/nm-manager.c | 2 | ||||
-rw-r--r-- | src/platform/nm-fake-platform.c | 215 | ||||
-rw-r--r-- | src/platform/nm-linux-platform.c | 219 | ||||
-rw-r--r-- | src/platform/nm-platform.c | 283 | ||||
-rw-r--r-- | src/platform/nm-platform.h | 47 | ||||
-rw-r--r-- | src/platform/tests/platform.c | 2 | ||||
-rw-r--r-- | src/platform/tests/test-address.c | 2 | ||||
-rw-r--r-- | src/platform/tests/test-cleanup.c | 2 | ||||
-rw-r--r-- | src/platform/tests/test-link.c | 33 | ||||
-rw-r--r-- | src/platform/tests/test-route.c | 2 | ||||
-rw-r--r-- | src/tests/test-route-manager.c | 4 |
13 files changed, 293 insertions, 537 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 91f26fd558..34295f2743 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -1408,7 +1408,7 @@ get_new_connection_ifname (const GSList *existing, for (i = 0; i < 500; i++) { name = g_strdup_printf ("%s%d", prefix, i); - if (nm_platform_link_exists (NM_PLATFORM_GET, name)) + if (nm_platform_link_get_by_ifname (NM_PLATFORM_GET, name)) goto next; for (iter = existing, found = FALSE; iter; iter = g_slist_next (iter)) { diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 17da4947a8..c8356c53c1 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -1346,14 +1346,18 @@ device_link_changed (NMDevice *self) gboolean platform_unmanaged = FALSE; const char *udi; NMPlatformLink info; + const NMPlatformLink *pllink; int ifindex; priv->device_link_changed_id = 0; ifindex = nm_device_get_ifindex (self); - if (!nm_platform_link_get (NM_PLATFORM_GET, ifindex, &info)) + pllink = nm_platform_link_get (NM_PLATFORM_GET, ifindex); + if (!pllink) return G_SOURCE_REMOVE; + info = *pllink; + udi = nm_platform_link_get_udi (NM_PLATFORM_GET, info.ifindex); if (udi && g_strcmp0 (udi, priv->udi)) { /* Update UDI to what udev gives us */ @@ -1485,21 +1489,22 @@ static gboolean device_ip_link_changed (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - NMPlatformLink info; + const NMPlatformLink *pllink; int ip_ifindex; priv->device_ip_link_changed_id = 0; ip_ifindex = nm_device_get_ip_ifindex (self); - if (!nm_platform_link_get (NM_PLATFORM_GET, ip_ifindex, &info)) + pllink = nm_platform_link_get (NM_PLATFORM_GET, ip_ifindex); + if (!pllink) return G_SOURCE_REMOVE; - if (info.name[0] && g_strcmp0 (priv->ip_iface, info.name)) { + if (pllink->name[0] && g_strcmp0 (priv->ip_iface, pllink->name)) { _LOGI (LOGD_DEVICE, "interface index %d renamed ip_iface (%d) from '%s' to '%s'", priv->ifindex, nm_device_get_ip_ifindex (self), - priv->ip_iface, info.name); + priv->ip_iface, pllink->name); g_free (priv->ip_iface); - priv->ip_iface = g_strdup (info.name); + priv->ip_iface = g_strdup (pllink->name); g_object_notify (G_OBJECT (self), NM_DEVICE_IP_IFACE); update_for_ip_ifname_change (self); diff --git a/src/nm-manager.c b/src/nm-manager.c index 228b832945..4819ad28c2 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1038,7 +1038,7 @@ system_create_virtual_device (NMManager *self, NMConnection *connection, GError */ priv->ignore_link_added_cb++; - nm_owned = !nm_platform_link_exists (NM_PLATFORM_GET, iface); + nm_owned = !nm_platform_link_get_by_ifname (NM_PLATFORM_GET, iface); device = nm_device_factory_create_virtual_device_for_connection (factory, connection, diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index 1eac40d9e2..062ecfb2d5 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -49,7 +49,6 @@ typedef struct { NMPlatformLink link; char *udi; - GBytes *address; int vlan_id; int ib_p_key; struct in6_addr ip6_lladdr; @@ -142,7 +141,6 @@ link_init (NMFakePlatformLink *device, int ifindex, int type, const char *name) device->link.flags = NM_FLAGS_UNSET (device->link.flags, IFF_NOARP); break; } - device->address = NULL; } static NMFakePlatformLink * @@ -177,36 +175,51 @@ link_get_all (NMPlatform *platform) return links; } -static gboolean -_nm_platform_link_get (NMPlatform *platform, int ifindex, NMPlatformLink *l) +static const NMPlatformLink * +_nm_platform_link_get (NMPlatform *platform, int ifindex) { NMFakePlatformLink *device = link_get (platform, ifindex); - if (device && l) - *l = device->link; - return !!device; + return device ? &device->link : NULL; } -static gboolean +static const NMPlatformLink * +_nm_platform_link_get_by_ifname (NMPlatform *platform, const char *ifname) +{ + NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); + guint i; + + for (i = 0; i < priv->links->len; i++) { + NMFakePlatformLink *device = &g_array_index (priv->links, NMFakePlatformLink, i); + + if (!strcmp (device->link.name, ifname)) + return &device->link; + } + return NULL; +} + +static const NMPlatformLink * _nm_platform_link_get_by_address (NMPlatform *platform, gconstpointer address, - size_t length, - NMPlatformLink *l) + size_t length) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); guint i; + if ( length == 0 + || length > NM_UTILS_HWADDR_LEN_MAX + || !address) + g_return_val_if_reached (NULL); + for (i = 0; i < priv->links->len; i++) { NMFakePlatformLink *device = &g_array_index (priv->links, NMFakePlatformLink, i); - if ( device->address - && g_bytes_get_size (device->address) == length - && memcmp (g_bytes_get_data (device->address, NULL), address, length) == 0) { - *l = device->link; - return TRUE; + if ( device->link.addr.len == length + && memcmp (device->link.addr.data, address, length) == 0) { + return &device->link; } } - return FALSE; + return NULL; } static gboolean @@ -280,48 +293,10 @@ link_delete (NMPlatform *platform, int ifindex) return TRUE; } -static int -link_get_ifindex (NMPlatform *platform, const char *name) -{ - NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); - int i; - - for (i = 0; i < priv->links->len; i++) { - NMFakePlatformLink *device = &g_array_index (priv->links, NMFakePlatformLink, i); - - if (device && !g_strcmp0 (device->link.name, name)) - return device->link.ifindex; - } - - return 0; -} - -static const char * -link_get_name (NMPlatform *platform, int ifindex) -{ - NMFakePlatformLink *device = link_get (platform, ifindex); - - return device ? device->link.name : NULL; -} - -static NMLinkType -link_get_type (NMPlatform *platform, int ifindex) -{ - NMFakePlatformLink *device = link_get (platform, ifindex); - - return device ? device->link.type : NM_LINK_TYPE_NONE; -} - static const char * link_get_type_name (NMPlatform *platform, int ifindex) { - return type_to_type_name (link_get_type (platform, ifindex)); -} - -static gboolean -link_get_unmanaged (NMPlatform *platform, int ifindex, gboolean *managed) -{ - return FALSE; + return type_to_type_name (nm_platform_link_get_type (platform, ifindex)); } static void @@ -448,62 +423,24 @@ link_set_noarp (NMPlatform *platform, int ifindex) } static gboolean -link_is_up (NMPlatform *platform, int ifindex) -{ - NMFakePlatformLink *device = link_get (platform, ifindex); - - return device ? NM_FLAGS_HAS (device->link.flags, IFF_UP) : FALSE; -} - -static gboolean -link_is_connected (NMPlatform *platform, int ifindex) -{ - NMFakePlatformLink *device = link_get (platform, ifindex); - - return device ? device->link.connected : FALSE; -} - -static gboolean -link_uses_arp (NMPlatform *platform, int ifindex) -{ - NMFakePlatformLink *device = link_get (platform, ifindex); - - return device ? !NM_FLAGS_HAS (device->link.flags, IFF_NOARP) : FALSE; -} - -static gboolean link_set_address (NMPlatform *platform, int ifindex, gconstpointer addr, size_t len) { NMFakePlatformLink *device = link_get (platform, ifindex); - if (device->address) - g_bytes_unref (device->address); - - device->address = g_bytes_new (addr, len); - - link_changed (platform, link_get (platform, ifindex), TRUE); - - return TRUE; -} - -static gconstpointer -link_get_address (NMPlatform *platform, int ifindex, size_t *length) -{ - NMFakePlatformLink *device = link_get (platform, ifindex); - - if (!device || !device->address) { - if (length) - *length = 0; - return NULL; + if ( len == 0 + || len > NM_UTILS_HWADDR_LEN_MAX + || !addr) + g_return_val_if_reached (FALSE); + + if ( device->link.addr.len != len + || ( len > 0 + && memcmp (device->link.addr.data, addr, len) != 0)) { + memcpy (device->link.addr.data, addr, len); + device->link.addr.len = len; + link_changed (platform, link_get (platform, ifindex), TRUE); } - return g_bytes_get_data (device->address, length); -} - -static gboolean -link_get_permanent_address (NMPlatform *platform, int ifindex, guint8 *buf, size_t *length) -{ - return FALSE; + return TRUE; } static gboolean @@ -519,32 +456,6 @@ link_set_mtu (NMPlatform *platform, int ifindex, guint32 mtu) return !!device; } -static guint32 -link_get_mtu (NMPlatform *platform, int ifindex) -{ - NMFakePlatformLink *device = link_get (platform, ifindex); - - return device ? device->link.mtu : 0; -} - -static char * -link_get_physical_port_id (NMPlatform *platform, int ifindex) -{ - /* We call link_get just to cause an error to be set if @ifindex is bad. */ - link_get (platform, ifindex); - - return NULL; -} - -static guint -link_get_dev_id (NMPlatform *platform, int ifindex) -{ - /* We call link_get just to cause an error to be set if @ifindex is bad. */ - link_get (platform, ifindex); - - return 0; -} - static const char * link_get_udi (NMPlatform *platform, int ifindex) { @@ -556,15 +467,6 @@ link_get_udi (NMPlatform *platform, int ifindex) } static gboolean -link_get_wake_on_lan (NMPlatform *platform, int ifindex) -{ - /* We call link_get just to cause an error to be set if @ifindex is bad. */ - link_get (platform, ifindex); - - return FALSE; -} - -static gboolean link_get_driver_info (NMPlatform *platform, int ifindex, char **out_driver_name, @@ -578,9 +480,6 @@ link_get_driver_info (NMPlatform *platform, if (out_fw_version) *out_fw_version = NULL; - /* We call link_get just to cause an error to be set if @ifindex is bad. */ - link_get (platform, ifindex); - return TRUE; } @@ -659,16 +558,6 @@ link_release (NMPlatform *platform, int master_idx, int slave_idx) return TRUE; } -static int -link_get_master (NMPlatform *platform, int slave) -{ - NMFakePlatformLink *device = link_get (platform, slave); - - g_return_val_if_fail (device, FALSE); - - return device->link.master; -} - static gboolean master_set_option (NMPlatform *platform, int master, const char *option, const char *value) { @@ -709,7 +598,7 @@ vlan_add (NMPlatform *platform, const char *name, int parent, int vlan_id, guint if (!link_add (platform, name, NM_LINK_TYPE_VLAN, NULL, 0, NULL)) return FALSE; - device = link_get (platform, link_get_ifindex (platform, name)); + device = link_get (platform, nm_platform_link_get_ifindex (platform, name)); g_return_val_if_fail (device, FALSE); @@ -761,7 +650,7 @@ infiniband_partition_add (NMPlatform *platform, int parent, int p_key, NMPlatfor if (!link_add (platform, name, NM_LINK_TYPE_INFINIBAND, NULL, 0, out_link)) return FALSE; - device = link_get (platform, link_get_ifindex (platform, name)); + device = link_get (platform, nm_platform_link_get_ifindex (platform, name)); g_return_val_if_fail (device, FALSE); device->ib_p_key = p_key; @@ -1484,7 +1373,6 @@ nm_fake_platform_finalize (GObject *object) for (i = 0; i < priv->links->len; i++) { NMFakePlatformLink *device = &g_array_index (priv->links, NMFakePlatformLink, i); - g_bytes_unref (device->address); g_free (device->udi); } g_array_unref (priv->links); @@ -1511,15 +1399,12 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass) platform_class->sysctl_get = sysctl_get; platform_class->link_get = _nm_platform_link_get; + platform_class->link_get_by_ifname = _nm_platform_link_get_by_ifname; platform_class->link_get_by_address = _nm_platform_link_get_by_address; platform_class->link_get_all = link_get_all; platform_class->link_add = link_add; platform_class->link_delete = link_delete; - platform_class->link_get_ifindex = link_get_ifindex; - platform_class->link_get_name = link_get_name; - platform_class->link_get_type = link_get_type; platform_class->link_get_type_name = link_get_type_name; - platform_class->link_get_unmanaged = link_get_unmanaged; platform_class->link_get_udi = link_get_udi; @@ -1527,19 +1412,10 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass) platform_class->link_set_down = link_set_down; platform_class->link_set_arp = link_set_arp; platform_class->link_set_noarp = link_set_noarp; - platform_class->link_is_up = link_is_up; - platform_class->link_is_connected = link_is_connected; - platform_class->link_uses_arp = link_uses_arp; platform_class->link_set_address = link_set_address; - platform_class->link_get_address = link_get_address; - platform_class->link_get_permanent_address = link_get_permanent_address; - platform_class->link_get_mtu = link_get_mtu; platform_class->link_set_mtu = link_set_mtu; - platform_class->link_get_physical_port_id = link_get_physical_port_id; - platform_class->link_get_dev_id = link_get_dev_id; - platform_class->link_get_wake_on_lan = link_get_wake_on_lan; platform_class->link_get_driver_info = link_get_driver_info; platform_class->link_supports_carrier_detect = link_supports_carrier_detect; @@ -1547,7 +1423,6 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass) platform_class->link_enslave = link_enslave; platform_class->link_release = link_release; - platform_class->link_get_master = link_get_master; platform_class->master_set_option = master_set_option; platform_class->master_get_option = master_get_option; platform_class->slave_set_option = slave_set_option; diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 0ae35e3a08..9029cbf176 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -2508,15 +2508,26 @@ link_get_all (NMPlatform *platform) nmp_cache_id_init_object_type (NMP_CACHE_ID_STATIC, NMP_OBJECT_TYPE_LINK, TRUE)); } -static gboolean -_nm_platform_link_get (NMPlatform *platform, int ifindex, NMPlatformLink *l) +static const NMPlatformLink * +_nm_platform_link_get (NMPlatform *platform, int ifindex) { const NMPObject *obj; obj = cache_lookup_link (platform, ifindex); - if (obj && l) - *l = obj->link; - return !!obj; + return obj ? &obj->link : NULL; +} + +static const NMPlatformLink * +_nm_platform_link_get_by_ifname (NMPlatform *platform, + const char *ifname) +{ + const NMPObject *obj = NULL; + + if (ifname && *ifname) { + obj = nmp_cache_lookup_link_full (NM_LINUX_PLATFORM_GET_PRIVATE (platform)->cache, + 0, ifname, TRUE, NM_LINK_TYPE_NONE, NULL, NULL); + } + return obj ? &obj->link : NULL; } struct _nm_platform_link_get_by_address_data { @@ -2530,11 +2541,10 @@ _nm_platform_link_get_by_address_match_link (const NMPObject *obj, struct _nm_pl return obj->link.addr.len == d->length && !memcmp (obj->link.addr.data, d->address, d->length); } -static gboolean +static const NMPlatformLink * _nm_platform_link_get_by_address (NMPlatform *platform, gconstpointer address, - size_t length, - NMPlatformLink *l) + size_t length) { const NMPObject *obj; struct _nm_platform_link_get_by_address_data d = { @@ -2543,16 +2553,14 @@ _nm_platform_link_get_by_address (NMPlatform *platform, }; if (length <= 0 || length > NM_UTILS_HWADDR_LEN_MAX) - return FALSE; + return NULL; if (!address) - return FALSE; + return NULL; obj = nmp_cache_lookup_link_full (NM_LINUX_PLATFORM_GET_PRIVATE (platform)->cache, 0, NULL, TRUE, NM_LINK_TYPE_NONE, (NMPObjectMatchFn) _nm_platform_link_get_by_address_match_link, &d); - if (obj && l) - *l = obj->link; - return !!obj; + return obj ? &obj->link : NULL; } static struct nl_object * @@ -2798,34 +2806,6 @@ link_delete (NMPlatform *platform, int ifindex) return do_delete_object (platform, &obj_needle, NULL); } -static int -link_get_ifindex (NMPlatform *platform, const char *ifname) -{ - const NMPObject *obj; - - g_return_val_if_fail (ifname, 0); - - obj = nmp_cache_lookup_link_full (NM_LINUX_PLATFORM_GET_PRIVATE (platform)->cache, - 0, ifname, TRUE, NM_LINK_TYPE_NONE, NULL, NULL); - return obj ? obj->link.ifindex : 0; -} - -static const char * -link_get_name (NMPlatform *platform, int ifindex) -{ - const NMPObject *obj = cache_lookup_link (platform, ifindex); - - return obj ? obj->link.name : NULL; -} - -static NMLinkType -link_get_type (NMPlatform *platform, int ifindex) -{ - const NMPObject *obj = cache_lookup_link (platform, ifindex); - - return obj ? obj->link.type : NM_LINK_TYPE_NONE; -} - static const char * link_get_type_name (NMPlatform *platform, int ifindex) { @@ -2866,14 +2846,6 @@ link_get_unmanaged (NMPlatform *platform, int ifindex, gboolean *managed) return FALSE; } -static guint32 -link_get_flags (NMPlatform *platform, int ifindex) -{ - const NMPObject *obj = cache_lookup_link (platform, ifindex); - - return obj ? obj->link.flags : IFF_NOARP; -} - static gboolean link_refresh (NMPlatform *platform, int ifindex) { @@ -2881,26 +2853,6 @@ link_refresh (NMPlatform *platform, int ifindex) return !!cache_lookup_link (platform, ifindex); } -static gboolean -link_is_up (NMPlatform *platform, int ifindex) -{ - return !!(link_get_flags (platform, ifindex) & IFF_UP); -} - -static gboolean -link_is_connected (NMPlatform *platform, int ifindex) -{ - const NMPObject *obj = cache_lookup_link (platform, ifindex); - - return obj ? obj->link.connected : FALSE; -} - -static gboolean -link_uses_arp (NMPlatform *platform, int ifindex) -{ - return !(link_get_flags (platform, ifindex) & IFF_NOARP); -} - static NMPlatformError link_change_flags (NMPlatform *platform, int ifindex, unsigned int flags, gboolean value) { @@ -2955,20 +2907,6 @@ link_set_noarp (NMPlatform *platform, int ifindex) return link_change_flags (platform, ifindex, IFF_NOARP, TRUE) == NM_PLATFORM_ERROR_SUCCESS; } -static gboolean -link_get_ipv6_token (NMPlatform *platform, int ifindex, NMUtilsIPv6IfaceId *iid) -{ -#if HAVE_LIBNL_INET6_TOKEN - const NMPObject *obj = cache_lookup_link (platform, ifindex); - - if (obj && obj->link.inet6_token.is_valid) { - *iid = obj->link.inet6_token.iid; - return TRUE; - } -#endif - return FALSE; -} - static const char * link_get_udi (NMPlatform *platform, int ifindex) { @@ -2996,18 +2934,6 @@ link_get_udev_device (NMPlatform *platform, int ifindex) } static gboolean -link_get_user_ipv6ll_enabled (NMPlatform *platform, int ifindex) -{ -#if HAVE_LIBNL_INET6_ADDR_GEN_MODE - const NMPObject *obj = cache_lookup_link (platform, ifindex); - - if (obj && obj->link.inet6_addr_gen_mode_inv) - return (~obj->link.inet6_addr_gen_mode_inv) == IN6_ADDR_GEN_MODE_NONE; -#endif - return FALSE; -} - -static gboolean link_set_user_ipv6ll_enabled (NMPlatform *platform, int ifindex, gboolean enabled) { #if HAVE_LIBNL_INET6_ADDR_GEN_MODE @@ -3069,28 +2995,6 @@ link_set_address (NMPlatform *platform, int ifindex, gconstpointer address, size return do_change_link (platform, change, TRUE) == NM_PLATFORM_ERROR_SUCCESS; } -static gconstpointer -link_get_address (NMPlatform *platform, int ifindex, size_t *length) -{ - const NMPObject *obj = cache_lookup_link (platform, ifindex); - gconstpointer a = NULL; - guint8 l = 0; - - if (obj && obj->link.addr.len > 0) { - if (obj->link.addr.len > NM_UTILS_HWADDR_LEN_MAX) { - if (length) - *length = 0; - g_return_val_if_reached (NULL); - } - a = obj->link.addr.data; - l = obj->link.addr.len; - } - - if (length) - *length = l; - return a; -} - static gboolean link_get_permanent_address (NMPlatform *platform, int ifindex, @@ -3111,14 +3015,6 @@ link_set_mtu (NMPlatform *platform, int ifindex, guint32 mtu) return do_change_link (platform, change, TRUE) == NM_PLATFORM_ERROR_SUCCESS; } -static guint32 -link_get_mtu (NMPlatform *platform, int ifindex) -{ - const NMPObject *obj = cache_lookup_link (platform, ifindex); - - return obj ? obj->link.mtu : 0; -} - static char * link_get_physical_port_id (NMPlatform *platform, int ifindex) { @@ -3251,14 +3147,6 @@ link_release (NMPlatform *platform, int master, int slave) return link_enslave (platform, 0, slave); } -static int -link_get_master (NMPlatform *platform, int slave) -{ - const NMPObject *obj = cache_lookup_link (platform, slave); - - return obj ? obj->link.master : 0; -} - static char * link_option_path (NMPlatform *platform, int master, const char *category, const char *option) { @@ -3292,13 +3180,12 @@ link_get_option (NMPlatform *platform, int master, const char *category, const c static const char * master_category (NMPlatform *platform, int master) { - switch (link_get_type (platform, master)) { + switch (nm_platform_link_get_type (platform, master)) { case NM_LINK_TYPE_BRIDGE: return "bridge"; case NM_LINK_TYPE_BOND: return "bonding"; default: - g_return_val_if_reached (NULL); return NULL; } } @@ -3306,16 +3193,15 @@ master_category (NMPlatform *platform, int master) static const char * slave_category (NMPlatform *platform, int slave) { - int master = link_get_master (platform, slave); + int master = nm_platform_link_get_master (platform, slave); if (master <= 0) return NULL; - switch (link_get_type (platform, master)) { + switch (nm_platform_link_get_type (platform, master)) { case NM_LINK_TYPE_BRIDGE: return "brport"; default: - g_return_val_if_reached (NULL); return NULL; } } @@ -3761,8 +3647,8 @@ vxlan_get_properties (NMPlatform *platform, int ifindex, NMPlatformVxlanProperti err = _nl_link_parse_info_data (priv->nlh, ifindex, vxlan_info_data_parser, props); if (err != 0) { - warning ("(%s) could not read properties: %s", - link_get_name (platform, ifindex), nl_geterror (err)); + warning ("(%s) could not read vxlan properties: %s", + nm_platform_link_get_name (platform, ifindex), nl_geterror (err)); } return (err == 0); } @@ -3815,8 +3701,8 @@ gre_get_properties (NMPlatform *platform, int ifindex, NMPlatformGreProperties * err = _nl_link_parse_info_data (priv->nlh, ifindex, gre_info_data_parser, props); if (err != 0) { - warning ("(%s) could not read properties: %s", - link_get_name (platform, ifindex), nl_geterror (err)); + warning ("(%s) could not read gre properties: %s", + nm_platform_link_get_name (platform, ifindex), nl_geterror (err)); } return (err == 0); } @@ -3829,26 +3715,25 @@ wifi_get_wifi_data (NMPlatform *platform, int ifindex) wifi_data = g_hash_table_lookup (priv->wifi_data, GINT_TO_POINTER (ifindex)); if (!wifi_data) { - NMLinkType type; - const char *ifname; - - type = link_get_type (platform, ifindex); - ifname = link_get_name (platform, ifindex); - - if (type == NM_LINK_TYPE_WIFI) - wifi_data = wifi_utils_init (ifname, ifindex, TRUE); - else if (type == NM_LINK_TYPE_OLPC_MESH) { - /* The kernel driver now uses nl80211, but we force use of WEXT because - * the cfg80211 interactions are not quite ready to support access to - * mesh control through nl80211 just yet. - */ + const NMPlatformLink *pllink; + + pllink = nm_platform_link_get (platform, ifindex); + if (pllink) { + if (pllink->type == NM_LINK_TYPE_WIFI) + wifi_data = wifi_utils_init (pllink->name, ifindex, TRUE); + else if (pllink->type == NM_LINK_TYPE_OLPC_MESH) { + /* The kernel driver now uses nl80211, but we force use of WEXT because + * the cfg80211 interactions are not quite ready to support access to + * mesh control through nl80211 just yet. + */ #if HAVE_WEXT - wifi_data = wifi_wext_init (ifname, ifindex, FALSE); + wifi_data = wifi_wext_init (pllink->name, ifindex, FALSE); #endif - } + } - if (wifi_data) - g_hash_table_insert (priv->wifi_data, GINT_TO_POINTER (ifindex), wifi_data); + if (wifi_data) + g_hash_table_insert (priv->wifi_data, GINT_TO_POINTER (ifindex), wifi_data); + } } return wifi_data; @@ -3993,10 +3878,10 @@ mesh_set_ssid (NMPlatform *platform, int ifindex, const guint8 *ssid, gsize len) static gboolean link_get_wake_on_lan (NMPlatform *platform, int ifindex) { - NMLinkType type = link_get_type (platform, ifindex); + NMLinkType type = nm_platform_link_get_type (platform, ifindex); if (type == NM_LINK_TYPE_ETHERNET) - return nmp_utils_ethtool_get_wake_on_lan (link_get_name (platform, ifindex)); + return nmp_utils_ethtool_get_wake_on_lan (nm_platform_link_get_name (platform, ifindex)); else if (type == NM_LINK_TYPE_WIFI) { WifiData *wifi_data = wifi_get_wifi_data (platform, ifindex); @@ -5039,13 +4924,11 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass) platform_class->sysctl_get = sysctl_get; platform_class->link_get = _nm_platform_link_get; + platform_class->link_get_by_ifname = _nm_platform_link_get_by_ifname; platform_class->link_get_by_address = _nm_platform_link_get_by_address; platform_class->link_get_all = link_get_all; platform_class->link_add = link_add; platform_class->link_delete = link_delete; - platform_class->link_get_ifindex = link_get_ifindex; - platform_class->link_get_name = link_get_name; - platform_class->link_get_type = link_get_type; platform_class->link_get_type_name = link_get_type_name; platform_class->link_get_unmanaged = link_get_unmanaged; @@ -5055,21 +4938,14 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass) platform_class->link_set_down = link_set_down; platform_class->link_set_arp = link_set_arp; platform_class->link_set_noarp = link_set_noarp; - platform_class->link_is_up = link_is_up; - platform_class->link_is_connected = link_is_connected; - platform_class->link_uses_arp = link_uses_arp; platform_class->link_get_udi = link_get_udi; platform_class->link_get_udev_device = link_get_udev_device; - platform_class->link_get_ipv6_token = link_get_ipv6_token; - platform_class->link_get_user_ipv6ll_enabled = link_get_user_ipv6ll_enabled; platform_class->link_set_user_ipv6ll_enabled = link_set_user_ipv6ll_enabled; - platform_class->link_get_address = link_get_address; platform_class->link_set_address = link_set_address; platform_class->link_get_permanent_address = link_get_permanent_address; - platform_class->link_get_mtu = link_get_mtu; platform_class->link_set_mtu = link_set_mtu; platform_class->link_get_physical_port_id = link_get_physical_port_id; @@ -5082,7 +4958,6 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass) platform_class->link_enslave = link_enslave; platform_class->link_release = link_release; - platform_class->link_get_master = link_get_master; platform_class->master_set_option = master_set_option; platform_class->master_get_option = master_get_option; platform_class->slave_set_option = slave_set_option; diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index cd422f9f9f..296d0d6188 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -487,23 +487,39 @@ nm_platform_link_get_all (NMPlatform *self) * nm_platform_link_get: * @self: platform instance * @ifindex: ifindex of the link - * @link: (out): output NMPlatformLink structure. * - * If a link with given @ifindex exists, fill the given NMPlatformLink - * structure. + * Lookup the internal NMPlatformLink object. * - * Returns: %TRUE, if such a link exists, %FALSE otherwise. - * If the link does not exist, the content of @link is undefined. + * Returns: %NULL, if such a link exists or the internal + * platform link object. Do not modify the returned value. + * Also, be aware that any subsequent platform call might + * invalidated/modify the returned instance. **/ -gboolean -nm_platform_link_get (NMPlatform *self, int ifindex, NMPlatformLink *link) +const NMPlatformLink * +nm_platform_link_get (NMPlatform *self, int ifindex) { - _CHECK_SELF (self, klass, FALSE); + _CHECK_SELF (self, klass, NULL); - g_return_val_if_fail (ifindex > 0, FALSE); + g_return_val_if_fail (ifindex > 0, NULL); + + return klass->link_get (self, ifindex); +} + +/** + * nm_platform_link_get_by_ifname: + * @self: platform instance + * @ifname: the ifname + * + * Returns: the first #NMPlatformLink instance with the given name. + **/ +const NMPlatformLink * +nm_platform_link_get_by_ifname (NMPlatform *self, const char *ifname) +{ + _CHECK_SELF (self, klass, NULL); - g_return_val_if_fail (klass->link_get, FALSE); - return !!klass->link_get (self, ifindex, link); + g_return_val_if_fail (ifname && *ifname, NULL); + + return klass->link_get_by_ifname (self, ifname); } /** @@ -511,55 +527,43 @@ nm_platform_link_get (NMPlatform *self, int ifindex, NMPlatformLink *link) * @self: platform instance * @address: a pointer to the binary hardware address * @length: the size of @address in bytes - * @link: (out): output NMPlatformLink structure. - * - * If a link with given @address exists, fill the given NMPlatformLink - * structure. * - * Returns: %TRUE, if such a link exists, %FALSE otherwise. - * If the link does not exist, the content of @link is undefined. + * Returns: the first #NMPlatformLink object with a matching + * address. **/ -gboolean +const NMPlatformLink * nm_platform_link_get_by_address (NMPlatform *self, gconstpointer address, - size_t length, - NMPlatformLink *link) + size_t length) { - _CHECK_SELF (self, klass, FALSE); + _CHECK_SELF (self, klass, NULL); - g_return_val_if_fail (address != NULL, FALSE); - g_return_val_if_fail (length > 0, FALSE); - g_return_val_if_fail (link, FALSE); + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (length > 0, NULL); - g_return_val_if_fail (klass->link_get_by_address, FALSE); - return !!klass->link_get_by_address (self, address, length, link); + return klass->link_get_by_address (self, address, length); } static NMPlatformError _link_add_check_existing (NMPlatform *self, const char *name, NMLinkType type, NMPlatformLink *out_link) { - int ifindex; - NMPlatformLink pllink; - - ifindex = nm_platform_link_get_ifindex (self, name); - if (ifindex > 0) { - if (nm_platform_link_get (self, ifindex, &pllink)) { - gboolean wrong_type; - - wrong_type = type != NM_LINK_TYPE_NONE && pllink.type != type; - debug ("link: skip adding link due to existing interface '%s' of type %s%s%s", - name, - nm_link_type_to_string (pllink.type), - wrong_type ? ", expected " : "", - wrong_type ? nm_link_type_to_string (type) : ""); - if (out_link) - *out_link = pllink; - if (wrong_type) - return NM_PLATFORM_ERROR_WRONG_TYPE; - return NM_PLATFORM_ERROR_EXISTS; - } - /* strange, nm_platform_link_get_ifindex() returned a valid ifindex, but nm_platform_link_get() failed. - * This is unexpected... proceed with "SUCCESS". */ + const NMPlatformLink *pllink; + + pllink = nm_platform_link_get_by_ifname (self, name); + if (pllink) { + gboolean wrong_type; + + wrong_type = type != NM_LINK_TYPE_NONE && pllink->type != type; + debug ("link: skip adding link due to existing interface '%s' of type %s%s%s", + name, + nm_link_type_to_string (pllink->type), + wrong_type ? ", expected " : "", + wrong_type ? nm_link_type_to_string (type) : ""); + if (out_link) + *out_link = *pllink; + if (wrong_type) + return NM_PLATFORM_ERROR_WRONG_TYPE; + return NM_PLATFORM_ERROR_EXISTS; } return NM_PLATFORM_ERROR_SUCCESS; } @@ -624,25 +628,6 @@ nm_platform_dummy_add (NMPlatform *self, const char *name, NMPlatformLink *out_l } /** - * nm_platform_link_exists: - * @self: platform instance - * @name: Interface name - * - * Returns: %TRUE if an interface of this name exists, %FALSE otherwise. - */ -gboolean -nm_platform_link_exists (NMPlatform *self, const char *name) -{ - int ifindex; - - _CHECK_SELF (self, klass, FALSE); - - ifindex = nm_platform_link_get_ifindex (self, name); - - return ifindex > 0; -} - -/** * nm_platform_link_delete: * @self: platform instance * @ifindex: Interface index @@ -650,18 +635,17 @@ nm_platform_link_exists (NMPlatform *self, const char *name) gboolean nm_platform_link_delete (NMPlatform *self, int ifindex) { - const char *name; + const NMPlatformLink *pllink; _CHECK_SELF (self, klass, FALSE); - g_return_val_if_fail (klass->link_delete, FALSE); - - name = nm_platform_link_get_name (self, ifindex); - - if (!name) + if (ifindex <= 0) + return FALSE; + pllink = nm_platform_link_get (self, ifindex); + if (!pllink) return FALSE; - debug ("link: deleting '%s' (%d)", name, ifindex); + debug ("link: deleting '%s' (%d)", pllink->name, ifindex); return klass->link_delete (self, ifindex); } @@ -676,19 +660,10 @@ nm_platform_link_delete (NMPlatform *self, int ifindex) int nm_platform_link_get_ifindex (NMPlatform *self, const char *name) { - int ifindex; + const NMPlatformLink *pllink; - _CHECK_SELF (self, klass, 0); - - g_return_val_if_fail (name, 0); - g_return_val_if_fail (klass->link_get_ifindex, 0); - - ifindex = klass->link_get_ifindex (self, name); - - if (!ifindex) - debug ("link not found: %s", name); - - return ifindex; + pllink = nm_platform_link_get_by_ifname (self, name); + return pllink ? pllink->ifindex : 0; } /** @@ -702,20 +677,12 @@ nm_platform_link_get_ifindex (NMPlatform *self, const char *name) const char * nm_platform_link_get_name (NMPlatform *self, int ifindex) { - const char *name; + const NMPlatformLink *pllink; _CHECK_SELF (self, klass, NULL); - g_return_val_if_fail (klass->link_get_name, NULL); - - name = klass->link_get_name (self, ifindex); - - if (!name) { - debug ("link not found: %d", ifindex); - return FALSE; - } - - return name; + pllink = nm_platform_link_get (self, ifindex); + return pllink ? pllink->name : NULL; } /** @@ -729,11 +696,12 @@ nm_platform_link_get_name (NMPlatform *self, int ifindex) NMLinkType nm_platform_link_get_type (NMPlatform *self, int ifindex) { - _CHECK_SELF (self, klass, NM_LINK_TYPE_NONE); + const NMPlatformLink *pllink; - g_return_val_if_fail (klass->link_get_type, NM_LINK_TYPE_NONE); + _CHECK_SELF (self, klass, NM_LINK_TYPE_NONE); - return klass->link_get_type (self, ifindex); + pllink = nm_platform_link_get (self, ifindex); + return pllink ? pllink->type : NM_LINK_TYPE_NONE; } /** @@ -770,9 +738,9 @@ nm_platform_link_get_unmanaged (NMPlatform *self, int ifindex, gboolean *managed { _CHECK_SELF (self, klass, FALSE); - g_return_val_if_fail (klass->link_get_unmanaged, FALSE); - - return klass->link_get_unmanaged (self, ifindex, managed); + if (klass->link_get_unmanaged) + return klass->link_get_unmanaged (self, ifindex, managed); + return FALSE; } /** @@ -823,6 +791,15 @@ nm_platform_link_refresh (NMPlatform *self, int ifindex) return TRUE; } +static guint32 +_link_get_flags (NMPlatform *self, int ifindex) +{ + const NMPlatformLink *pllink; + + pllink = nm_platform_link_get (self, ifindex); + return pllink ? pllink->flags : IFF_NOARP; +} + /** * nm_platform_link_is_up: * @self: platform instance @@ -835,10 +812,7 @@ nm_platform_link_is_up (NMPlatform *self, int ifindex) { _CHECK_SELF (self, klass, FALSE); - g_return_val_if_fail (ifindex >= 0, FALSE); - g_return_val_if_fail (klass->link_is_up, FALSE); - - return klass->link_is_up (self, ifindex); + return NM_FLAGS_HAS (_link_get_flags (self, ifindex), IFF_UP); } /** @@ -851,12 +825,12 @@ nm_platform_link_is_up (NMPlatform *self, int ifindex) gboolean nm_platform_link_is_connected (NMPlatform *self, int ifindex) { - _CHECK_SELF (self, klass, FALSE); + const NMPlatformLink *pllink; - g_return_val_if_fail (ifindex >= 0, FALSE); - g_return_val_if_fail (klass->link_is_connected, FALSE); + _CHECK_SELF (self, klass, FALSE); - return klass->link_is_connected (self, ifindex); + pllink = nm_platform_link_get (self, ifindex); + return pllink ? pllink->connected : FALSE; } /** @@ -871,10 +845,7 @@ nm_platform_link_uses_arp (NMPlatform *self, int ifindex) { _CHECK_SELF (self, klass, FALSE); - g_return_val_if_fail (ifindex >= 0, FALSE); - g_return_val_if_fail (klass->link_uses_arp, FALSE); - - return klass->link_uses_arp (self, ifindex); + return !NM_FLAGS_HAS (_link_get_flags (self, ifindex), IFF_NOARP); } /** @@ -897,8 +868,17 @@ nm_platform_link_get_ipv6_token (NMPlatform *self, int ifindex, NMUtilsIPv6Iface g_return_val_if_fail (ifindex >= 0, FALSE); g_return_val_if_fail (iid, FALSE); - if (klass->link_get_ipv6_token) - return klass->link_get_ipv6_token (self, ifindex, iid); +#if HAVE_LIBNL_INET6_TOKEN + { + const NMPlatformLink *pllink; + + pllink = nm_platform_link_get (self, ifindex); + if (pllink && pllink->inet6_token.is_valid) { + *iid = pllink->inet6_token.iid; + return TRUE; + } + } +#endif return FALSE; } @@ -943,13 +923,20 @@ nm_platform_link_get_user_ipv6ll_enabled (NMPlatform *self, int ifindex) _CHECK_SELF (self, klass, FALSE); g_return_val_if_fail (ifindex >= 0, FALSE); - g_return_val_if_fail (klass->check_support_user_ipv6ll, FALSE); - if (klass->link_get_user_ipv6ll_enabled) - return klass->link_get_user_ipv6ll_enabled (self, ifindex); +#if HAVE_LIBNL_INET6_ADDR_GEN_MODE + { + const NMPlatformLink *pllink; + + pllink = nm_platform_link_get (self, ifindex); + if (pllink && pllink->inet6_addr_gen_mode_inv) + return (~pllink->inet6_addr_gen_mode_inv) == IN6_ADDR_GEN_MODE_NONE; + } +#endif return FALSE; } + /** * nm_platform_link_set_user_ip6vll_enabled: * @self: platform instance @@ -1008,15 +995,31 @@ nm_platform_link_set_address (NMPlatform *self, int ifindex, gconstpointer addre gconstpointer nm_platform_link_get_address (NMPlatform *self, int ifindex, size_t *length) { + const NMPlatformLink *pllink; + gconstpointer a = NULL; + guint8 l = 0; + _CHECK_SELF (self, klass, NULL); if (length) *length = 0; g_return_val_if_fail (ifindex > 0, NULL); - g_return_val_if_fail (klass->link_get_address, NULL); - return klass->link_get_address (self, ifindex, length); + pllink = nm_platform_link_get (self, ifindex); + if (pllink && pllink->addr.len > 0) { + if (pllink->addr.len > NM_UTILS_HWADDR_LEN_MAX) { + if (length) + *length = 0; + g_return_val_if_reached (NULL); + } + a = pllink->addr.data; + l = pllink->addr.len; + } + + if (length) + *length = l; + return a; } /** @@ -1039,11 +1042,12 @@ nm_platform_link_get_permanent_address (NMPlatform *self, int ifindex, guint8 *b *length = 0; g_return_val_if_fail (ifindex > 0, FALSE); - g_return_val_if_fail (klass->link_get_permanent_address, FALSE); g_return_val_if_fail (buf, FALSE); g_return_val_if_fail (length, FALSE); - return klass->link_get_permanent_address (self, ifindex, buf, length); + if (klass->link_get_permanent_address) + return klass->link_get_permanent_address (self, ifindex, buf, length); + return FALSE; } gboolean @@ -1176,12 +1180,12 @@ nm_platform_link_set_mtu (NMPlatform *self, int ifindex, guint32 mtu) guint32 nm_platform_link_get_mtu (NMPlatform *self, int ifindex) { - _CHECK_SELF (self, klass, 0); + const NMPlatformLink *pllink; - g_return_val_if_fail (ifindex >= 0, 0); - g_return_val_if_fail (klass->link_get_mtu, 0); + _CHECK_SELF (self, klass, 0); - return klass->link_get_mtu (self, ifindex); + pllink = nm_platform_link_get (self, ifindex); + return pllink ? pllink->mtu : 0; } /** @@ -1204,9 +1208,10 @@ nm_platform_link_get_physical_port_id (NMPlatform *self, int ifindex) _CHECK_SELF (self, klass, NULL); g_return_val_if_fail (ifindex >= 0, NULL); - g_return_val_if_fail (klass->link_get_physical_port_id, NULL); - return klass->link_get_physical_port_id (self, ifindex); + if (klass->link_get_physical_port_id) + return klass->link_get_physical_port_id (self, ifindex); + return NULL; } /** @@ -1227,9 +1232,10 @@ nm_platform_link_get_dev_id (NMPlatform *self, int ifindex) _CHECK_SELF (self, klass, 0); g_return_val_if_fail (ifindex >= 0, 0); - g_return_val_if_fail (klass->link_get_dev_id, 0); - return klass->link_get_dev_id (self, ifindex); + if (klass->link_get_dev_id) + return klass->link_get_dev_id (self, ifindex); + return 0; } /** @@ -1245,9 +1251,10 @@ nm_platform_link_get_wake_on_lan (NMPlatform *self, int ifindex) _CHECK_SELF (self, klass, FALSE); g_return_val_if_fail (ifindex >= 0, FALSE); - g_return_val_if_fail (klass->link_get_wake_on_lan, FALSE); - return klass->link_get_wake_on_lan (self, ifindex); + if (klass->link_get_wake_on_lan) + return klass->link_get_wake_on_lan (self, ifindex); + return FALSE; } /** @@ -1340,14 +1347,14 @@ nm_platform_link_release (NMPlatform *self, int master, int slave) int nm_platform_link_get_master (NMPlatform *self, int slave) { + const NMPlatformLink *pllink; + _CHECK_SELF (self, klass, 0); g_return_val_if_fail (slave >= 0, FALSE); - g_return_val_if_fail (klass->link_get_master, FALSE); - if (!nm_platform_link_get_name (self, slave)) - return 0; - return klass->link_get_master (self, slave); + pllink = nm_platform_link_get (self, slave); + return pllink ? pllink->master : 0; } /** diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index ae9f450cdd..923ff7fa11 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -409,8 +409,10 @@ typedef struct { gboolean (*sysctl_set) (NMPlatform *, const char *path, const char *value); char * (*sysctl_get) (NMPlatform *, const char *path); - gboolean (*link_get) (NMPlatform *platform, int ifindex, NMPlatformLink *link); - gboolean (*link_get_by_address) (NMPlatform *platform, gconstpointer address, size_t length, NMPlatformLink *link); + const NMPlatformLink *(*link_get) (NMPlatform *platform, int ifindex); + const NMPlatformLink *(*link_get_by_ifname) (NMPlatform *platform, const char *ifname); + const NMPlatformLink *(*link_get_by_address) (NMPlatform *platform, gconstpointer address, size_t length); + GArray *(*link_get_all) (NMPlatform *); gboolean (*link_add) (NMPlatform *, const char *name, @@ -419,9 +421,6 @@ typedef struct { size_t address_len, NMPlatformLink *out_link); gboolean (*link_delete) (NMPlatform *, int ifindex); - int (*link_get_ifindex) (NMPlatform *, const char *name); - const char *(*link_get_name) (NMPlatform *, int ifindex); - NMLinkType (*link_get_type) (NMPlatform *, int ifindex); const char *(*link_get_type_name) (NMPlatform *, int ifindex); gboolean (*link_get_unmanaged) (NMPlatform *, int ifindex, gboolean *managed); @@ -432,24 +431,17 @@ typedef struct { gboolean (*link_set_down) (NMPlatform *, int ifindex); gboolean (*link_set_arp) (NMPlatform *, int ifindex); gboolean (*link_set_noarp) (NMPlatform *, int ifindex); - gboolean (*link_is_up) (NMPlatform *, int ifindex); - gboolean (*link_is_connected) (NMPlatform *, int ifindex); - gboolean (*link_uses_arp) (NMPlatform *, int ifindex); const char *(*link_get_udi) (NMPlatform *self, int ifindex); GObject *(*link_get_udev_device) (NMPlatform *self, int ifindex); - gboolean (*link_get_ipv6_token) (NMPlatform *, int ifindex, NMUtilsIPv6IfaceId *iid); - gboolean (*link_get_user_ipv6ll_enabled) (NMPlatform *, int ifindex); gboolean (*link_set_user_ipv6ll_enabled) (NMPlatform *, int ifindex, gboolean enabled); - gconstpointer (*link_get_address) (NMPlatform *, int ifindex, size_t *length); gboolean (*link_get_permanent_address) (NMPlatform *, int ifindex, guint8 *buf, size_t *length); gboolean (*link_set_address) (NMPlatform *, int ifindex, gconstpointer address, size_t length); - guint32 (*link_get_mtu) (NMPlatform *, int ifindex); gboolean (*link_set_mtu) (NMPlatform *, int ifindex, guint32 mtu); char * (*link_get_physical_port_id) (NMPlatform *, int ifindex); @@ -466,7 +458,6 @@ typedef struct { gboolean (*link_enslave) (NMPlatform *, int master, int slave); gboolean (*link_release) (NMPlatform *, int master, int slave); - gboolean (*link_get_master) (NMPlatform *, int slave); gboolean (*master_set_option) (NMPlatform *, int ifindex, const char *option, const char *value); char * (*master_get_option) (NMPlatform *, int ifindex, const char *option); gboolean (*slave_set_option) (NMPlatform *, int ifindex, const char *option, const char *value); @@ -597,22 +588,34 @@ gint64 nm_platform_sysctl_get_int_checked (NMPlatform *self, const char *path, g gboolean nm_platform_sysctl_set_ip6_hop_limit_safe (NMPlatform *self, const char *iface, int value); -gboolean nm_platform_link_get (NMPlatform *self, int ifindex, NMPlatformLink *link); +const NMPlatformLink *nm_platform_link_get (NMPlatform *self, int ifindex); +const NMPlatformLink *nm_platform_link_get_by_ifname (NMPlatform *self, const char *ifname); +const NMPlatformLink *nm_platform_link_get_by_address (NMPlatform *self, gconstpointer address, size_t length); + GArray *nm_platform_link_get_all (NMPlatform *self); -gboolean nm_platform_link_get_by_address (NMPlatform *self, gconstpointer address, size_t length, NMPlatformLink *link); NMPlatformError nm_platform_dummy_add (NMPlatform *self, const char *name, NMPlatformLink *out_link); NMPlatformError nm_platform_bridge_add (NMPlatform *self, const char *name, const void *address, size_t address_len, NMPlatformLink *out_link); NMPlatformError nm_platform_bond_add (NMPlatform *self, const char *name, NMPlatformLink *out_link); NMPlatformError nm_platform_team_add (NMPlatform *self, const char *name, NMPlatformLink *out_link); -gboolean nm_platform_link_exists (NMPlatform *self, const char *name); gboolean nm_platform_link_delete (NMPlatform *self, int ifindex); + +/* convienience methods to lookup the link and access fields of NMPlatformLink. */ int nm_platform_link_get_ifindex (NMPlatform *self, const char *name); const char *nm_platform_link_get_name (NMPlatform *self, int ifindex); NMLinkType nm_platform_link_get_type (NMPlatform *self, int ifindex); -const char *nm_platform_link_get_type_name (NMPlatform *self, int ifindex); -gboolean nm_platform_link_get_unmanaged (NMPlatform *self, int ifindex, gboolean *managed); gboolean nm_platform_link_is_software (NMPlatform *self, int ifindex); +gboolean nm_platform_link_is_up (NMPlatform *self, int ifindex); +gboolean nm_platform_link_is_connected (NMPlatform *self, int ifindex); +gboolean nm_platform_link_uses_arp (NMPlatform *self, int ifindex); +guint32 nm_platform_link_get_mtu (NMPlatform *self, int ifindex); +gboolean nm_platform_link_get_ipv6_token (NMPlatform *self, int ifindex, NMUtilsIPv6IfaceId *iid); +gboolean nm_platform_link_get_user_ipv6ll_enabled (NMPlatform *self, int ifindex); +gconstpointer nm_platform_link_get_address (NMPlatform *self, int ifindex, size_t *length); +int nm_platform_link_get_master (NMPlatform *self, int slave); + +gboolean nm_platform_link_get_unmanaged (NMPlatform *self, int ifindex, gboolean *managed); gboolean nm_platform_link_supports_slaves (NMPlatform *self, int ifindex); +const char *nm_platform_link_get_type_name (NMPlatform *self, int ifindex); gboolean nm_platform_link_refresh (NMPlatform *self, int ifindex); void nm_platform_process_events (NMPlatform *self); @@ -621,22 +624,15 @@ gboolean nm_platform_link_set_up (NMPlatform *self, int ifindex, gboolean *out_n gboolean nm_platform_link_set_down (NMPlatform *self, int ifindex); gboolean nm_platform_link_set_arp (NMPlatform *self, int ifindex); gboolean nm_platform_link_set_noarp (NMPlatform *self, int ifindex); -gboolean nm_platform_link_is_up (NMPlatform *self, int ifindex); -gboolean nm_platform_link_is_connected (NMPlatform *self, int ifindex); -gboolean nm_platform_link_uses_arp (NMPlatform *self, int ifindex); -gboolean nm_platform_link_get_ipv6_token (NMPlatform *self, int ifindex, NMUtilsIPv6IfaceId *iid); const char *nm_platform_link_get_udi (NMPlatform *self, int ifindex); GObject *nm_platform_link_get_udev_device (NMPlatform *self, int ifindex); -gboolean nm_platform_link_get_user_ipv6ll_enabled (NMPlatform *self, int ifindex); gboolean nm_platform_link_set_user_ipv6ll_enabled (NMPlatform *self, int ifindex, gboolean enabled); -gconstpointer nm_platform_link_get_address (NMPlatform *self, int ifindex, size_t *length); gboolean nm_platform_link_get_permanent_address (NMPlatform *self, int ifindex, guint8 *buf, size_t *length); gboolean nm_platform_link_set_address (NMPlatform *self, int ifindex, const void *address, size_t length); -guint32 nm_platform_link_get_mtu (NMPlatform *self, int ifindex); gboolean nm_platform_link_set_mtu (NMPlatform *self, int ifindex, guint32 mtu); char *nm_platform_link_get_physical_port_id (NMPlatform *self, int ifindex); @@ -653,7 +649,6 @@ gboolean nm_platform_link_supports_vlans (NMPlatform *self, int ifindex); gboolean nm_platform_link_enslave (NMPlatform *self, int master, int slave); gboolean nm_platform_link_release (NMPlatform *self, int master, int slave); -int nm_platform_link_get_master (NMPlatform *self, int slave); gboolean nm_platform_master_set_option (NMPlatform *self, int ifindex, const char *option, const char *value); char *nm_platform_master_get_option (NMPlatform *self, int ifindex, const char *option); gboolean nm_platform_slave_set_option (NMPlatform *self, int ifindex, const char *option, const char *value); diff --git a/src/platform/tests/platform.c b/src/platform/tests/platform.c index fe148518c2..f87588a30b 100644 --- a/src/platform/tests/platform.c +++ b/src/platform/tests/platform.c @@ -128,7 +128,7 @@ do_vlan_add (char **argv) static gboolean do_link_exists (char **argv) { - gboolean value = nm_platform_link_exists (NM_PLATFORM_GET, argv[0]); + gboolean value = !!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, argv[0]); print_boolean (value); diff --git a/src/platform/tests/test-address.c b/src/platform/tests/test-address.c index 6d7a47def4..ea3ceb269b 100644 --- a/src/platform/tests/test-address.c +++ b/src/platform/tests/test-address.c @@ -236,7 +236,7 @@ setup_tests (void) SignalData *link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, DEVICE_NAME); nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); g_assert (nm_platform_dummy_add (NM_PLATFORM_GET, DEVICE_NAME, NULL) == NM_PLATFORM_ERROR_SUCCESS); accept_signal (link_added); free_signal (link_added); diff --git a/src/platform/tests/test-cleanup.c b/src/platform/tests/test-cleanup.c index 640d3a6f95..f14d9c07ac 100644 --- a/src/platform/tests/test-cleanup.c +++ b/src/platform/tests/test-cleanup.c @@ -96,7 +96,7 @@ void setup_tests (void) { nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); g_test_add_func ("/internal", test_cleanup_internal); /* FIXME: add external cleanup check */ diff --git a/src/platform/tests/test-link.c b/src/platform/tests/test-link.c index e09e1eb9d1..dc7b83975d 100644 --- a/src/platform/tests/test-link.c +++ b/src/platform/tests/test-link.c @@ -21,7 +21,7 @@ test_bogus(void) { size_t addrlen; - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, BOGUS_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, BOGUS_NAME)); g_assert (!nm_platform_link_delete (NM_PLATFORM_GET, BOGUS_IFINDEX)); g_assert (!nm_platform_link_get_ifindex (NM_PLATFORM_GET, BOGUS_NAME)); g_assert (!nm_platform_link_get_name (NM_PLATFORM_GET, BOGUS_IFINDEX)); @@ -53,7 +53,7 @@ test_bogus(void) static void test_loopback (void) { - g_assert (nm_platform_link_exists (NM_PLATFORM_GET, LO_NAME)); + g_assert (nm_platform_link_get_by_ifname (NM_PLATFORM_GET, LO_NAME)); g_assert_cmpint (nm_platform_link_get_type (NM_PLATFORM_GET, LO_INDEX), ==, NM_LINK_TYPE_LOOPBACK); g_assert_cmpint (nm_platform_link_get_ifindex (NM_PLATFORM_GET, LO_NAME), ==, LO_INDEX); g_assert_cmpstr (nm_platform_link_get_name (NM_PLATFORM_GET, LO_INDEX), ==, LO_NAME); @@ -73,14 +73,14 @@ software_add (NMLinkType link_type, const char *name) return nm_platform_bridge_add (NM_PLATFORM_GET, name, NULL, 0, NULL) == NM_PLATFORM_ERROR_SUCCESS; case NM_LINK_TYPE_BOND: { - gboolean bond0_exists = nm_platform_link_exists (NM_PLATFORM_GET, "bond0"); + gboolean bond0_exists = !!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, "bond0"); NMPlatformError plerr; plerr = nm_platform_bond_add (NM_PLATFORM_GET, name, NULL); /* Check that bond0 is *not* automatically created. */ if (!bond0_exists) - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, "bond0")); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, "bond0")); return plerr == NM_PLATFORM_ERROR_SUCCESS; } case NM_LINK_TYPE_TEAM: @@ -269,7 +269,7 @@ test_software (NMLinkType link_type, const char *link_typename) link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, DEVICE_NAME); g_assert (software_add (link_type, DEVICE_NAME)); accept_signal (link_added); - g_assert (nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME); g_assert (ifindex >= 0); g_assert_cmpint (nm_platform_link_get_type (NM_PLATFORM_GET, ifindex), ==, link_type); @@ -333,7 +333,7 @@ test_software (NMLinkType link_type, const char *link_typename) /* Delete */ g_assert (nm_platform_link_delete (NM_PLATFORM_GET, ifindex)); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); g_assert_cmpint (nm_platform_link_get_type (NM_PLATFORM_GET, ifindex), ==, NM_LINK_TYPE_NONE); g_assert (!nm_platform_link_get_type (NM_PLATFORM_GET, ifindex)); accept_signal (link_removed); @@ -397,7 +397,7 @@ test_internal (void) int ifindex; /* Check the functions for non-existent devices */ - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); g_assert (!nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); /* Add device */ @@ -470,16 +470,15 @@ test_internal (void) static void test_external (void) { - NMPlatformLink link; + const NMPlatformLink *pllink; SignalData *link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, DEVICE_NAME); SignalData *link_changed, *link_removed; int ifindex; - gboolean success; run_command ("ip link add %s type %s", DEVICE_NAME, "dummy"); wait_signal (link_added); - g_assert (nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME); g_assert (ifindex > 0); g_assert_cmpstr (nm_platform_link_get_name (NM_PLATFORM_GET, ifindex), ==, DEVICE_NAME); @@ -488,9 +487,9 @@ test_external (void) link_changed = add_signal_ifindex (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, link_callback, ifindex); link_removed = add_signal_ifindex (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, link_callback, ifindex); - success = nm_platform_link_get (NM_PLATFORM_GET, ifindex, &link); - g_assert (success); - if (!link.initialized) { + pllink = nm_platform_link_get (NM_PLATFORM_GET, ifindex); + g_assert (pllink); + if (!pllink->initialized) { /* we still lack the notification via UDEV. Expect another link changed signal. */ wait_signal (link_changed); } @@ -520,7 +519,7 @@ test_external (void) run_command ("ip link del %s", DEVICE_NAME); wait_signal (link_removed); accept_signals (link_changed, 0, 1); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); free_signal (link_added); free_signal (link_changed); @@ -539,9 +538,9 @@ setup_tests (void) nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, SLAVE_NAME)); nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, PARENT_NAME)); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, SLAVE_NAME)); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, PARENT_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, SLAVE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, PARENT_NAME)); g_test_add_func ("/link/bogus", test_bogus); g_test_add_func ("/link/loopback", test_loopback); diff --git a/src/platform/tests/test-route.c b/src/platform/tests/test-route.c index cd4217d248..81ec27b472 100644 --- a/src/platform/tests/test-route.c +++ b/src/platform/tests/test-route.c @@ -294,7 +294,7 @@ setup_tests (void) SignalData *link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, DEVICE_NAME); nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME)); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, DEVICE_NAME)); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, DEVICE_NAME)); g_assert (nm_platform_dummy_add (NM_PLATFORM_GET, DEVICE_NAME, NULL) == NM_PLATFORM_ERROR_SUCCESS); accept_signal (link_added); free_signal (link_added); diff --git a/src/tests/test-route-manager.c b/src/tests/test-route-manager.c index 85397ae7fb..60199c523f 100644 --- a/src/tests/test-route-manager.c +++ b/src/tests/test-route-manager.c @@ -647,7 +647,7 @@ fixture_setup (test_fixture *fixture, gconstpointer user_data) link_callback, "nm-test-device0"); nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, "nm-test-device0")); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, "nm-test-device0")); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, "nm-test-device0")); g_assert (nm_platform_dummy_add (NM_PLATFORM_GET, "nm-test-device0", NULL) == NM_PLATFORM_ERROR_SUCCESS); accept_signal (link_added); free_signal (link_added); @@ -659,7 +659,7 @@ fixture_setup (test_fixture *fixture, gconstpointer user_data) link_callback, "nm-test-device1"); nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, "nm-test-device1")); - g_assert (!nm_platform_link_exists (NM_PLATFORM_GET, "nm-test-device1")); + g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, "nm-test-device1")); g_assert (nm_platform_dummy_add (NM_PLATFORM_GET, "nm-test-device1", NULL) == NM_PLATFORM_ERROR_SUCCESS); accept_signal (link_added); free_signal (link_added); |