summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-12-15 16:23:05 +0100
committerThomas Haller <thaller@redhat.com>2015-12-17 12:06:25 +0100
commitb559f10908e7656054c3685f0cee4e0e1cb41b89 (patch)
treecc78da4ddc4abc64e7d69494ea690e64553213cb
parente6bd4ee2d1759230d708ef64d6edeba8ee0f0c47 (diff)
downloadNetworkManager-b559f10908e7656054c3685f0cee4e0e1cb41b89.tar.gz
platform: add index for links by ifname
Downsides: - Add some additional overhead to manage the index - The NMPCacheId struct grows to 16 bytes, affecting hashing performance for all object types. Still do it, based on the assumption that it doesn't matter for a low number of interfaces. But the O(1) access time matters when having lots of interfaces.
-rw-r--r--src/platform/nmp-object.c47
-rw-r--r--src/platform/nmp-object.h9
2 files changed, 54 insertions, 2 deletions
diff --git a/src/platform/nmp-object.c b/src/platform/nmp-object.c
index 2b1ec2127f..6fbbbe2357 100644
--- a/src/platform/nmp-object.c
+++ b/src/platform/nmp-object.c
@@ -1072,6 +1072,24 @@ nmp_cache_id_init_routes_visible (NMPCacheId *id,
return id;
}
+NMPCacheId *
+nmp_cache_id_init_link_by_ifname (NMPCacheId *id,
+ const char *ifname)
+{
+ gsize l;
+
+ _nmp_cache_id_init (id, NMP_CACHE_ID_TYPE_LINK_BY_IFNAME);
+
+ if ( !ifname
+ || (l = strlen (ifname)) > sizeof (id->link_by_ifname.ifname_short))
+ g_return_val_if_reached (id);
+
+ /* the trailing NUL is dropped!! */
+ memcpy (id->link_by_ifname.ifname_short, ifname, l);
+
+ return id;
+}
+
/******************************************************************/
static gboolean
@@ -1096,6 +1114,23 @@ _nmp_object_init_cache_id (const NMPObject *obj, NMPCacheIdType id_type, NMPCach
}
static gboolean
+_vt_cmd_obj_init_cache_id_link (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
+{
+ switch (id_type) {
+ case NMP_CACHE_ID_TYPE_LINK_BY_IFNAME:
+ if (obj->link.name[0]) {
+ *out_id = nmp_cache_id_init_link_by_ifname (id, obj->link.name);
+ return TRUE;
+ }
+ break;
+ default:
+ return FALSE;
+ }
+ *out_id = NULL;
+ return TRUE;
+}
+
+static gboolean
_vt_cmd_obj_init_cache_id_ipx_address (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
{
switch (id_type) {
@@ -1356,7 +1391,7 @@ nmp_cache_lookup_link_full (const NMPCache *cache,
const NMPObject *obj;
const NMPlatformObject *const *list;
guint i, len;
- NMPCacheId cache_id;
+ NMPCacheId cache_id, *p_cache_id;
if (ifindex > 0) {
obj = nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&obj_needle, ifindex));
@@ -1371,7 +1406,14 @@ nmp_cache_lookup_link_full (const NMPCache *cache,
} else if (!ifname && !match_fn)
return NULL;
else {
- list = nmp_cache_lookup_multi (cache, nmp_cache_id_init_object_type (&cache_id, NMP_OBJECT_TYPE_LINK, visible_only), &len);
+ if ( ifname
+ && strlen (ifname) <= sizeof (cache_id.link_by_ifname.ifname_short)) {
+ p_cache_id = nmp_cache_id_init_link_by_ifname (&cache_id, ifname);
+ ifname = NULL;
+ } else
+ p_cache_id = nmp_cache_id_init_object_type (&cache_id, NMP_OBJECT_TYPE_LINK, visible_only);
+
+ list = nmp_cache_lookup_multi (cache, p_cache_id, &len);
for (i = 0; i < len; i++) {
obj = NMP_OBJECT_UP_CAST (list[i]);
@@ -1933,6 +1975,7 @@ const NMPClass _nmp_classes[NMP_OBJECT_TYPE_MAX] = {
.rtm_gettype = RTM_GETLINK,
.signal_type_id = NM_PLATFORM_SIGNAL_ID_LINK,
.signal_type = NM_PLATFORM_SIGNAL_LINK_CHANGED,
+ .cmd_obj_init_cache_id = _vt_cmd_obj_init_cache_id_link,
.cmd_obj_cmp = _vt_cmd_obj_cmp_link,
.cmd_obj_copy = _vt_cmd_obj_copy_link,
.cmd_obj_stackinit_id = _vt_cmd_obj_stackinit_id_link,
diff --git a/src/platform/nmp-object.h b/src/platform/nmp-object.h
index 5acde46075..ab1cc2fe8e 100644
--- a/src/platform/nmp-object.h
+++ b/src/platform/nmp-object.h
@@ -64,6 +64,9 @@ typedef enum { /*< skip >*/
/* all the objects of a certain type */
NMP_CACHE_ID_TYPE_OBJECT_TYPE,
+ /* index for the link objects by ifname. */
+ NMP_CACHE_ID_TYPE_LINK_BY_IFNAME,
+
/* all the visible objects of a certain type */
NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY,
@@ -104,6 +107,11 @@ typedef struct {
guint8 obj_type; /* NMPObjectType as guint8 */
int ifindex;
} object_type_by_ifindex;
+ struct {
+ /* NMP_CACHE_ID_TYPE_LINK_BY_IFNAME */
+ guint8 _id_type;
+ char ifname_short[IFNAMSIZ - 1]; /* don't include the trailing NUL so the struct fits in 4 bytes. */
+ } link_by_ifname;
};
} NMPCacheId;
@@ -374,6 +382,7 @@ void nmp_cache_id_destroy (NMPCacheId *id);
NMPCacheId *nmp_cache_id_init_object_type (NMPCacheId *id, NMPObjectType obj_type, gboolean visible_only);
NMPCacheId *nmp_cache_id_init_addrroute_visible_by_ifindex (NMPCacheId *id, NMPObjectType obj_type, int ifindex);
NMPCacheId *nmp_cache_id_init_routes_visible (NMPCacheId *id, NMPObjectType obj_type, gboolean with_default, gboolean with_non_default, int ifindex);
+NMPCacheId *nmp_cache_id_init_link_by_ifname (NMPCacheId *id, const char *ifname);
const NMPlatformObject *const *nmp_cache_lookup_multi (const NMPCache *cache, const NMPCacheId *cache_id, guint *out_len);
GArray *nmp_cache_lookup_multi_to_array (const NMPCache *cache, NMPObjectType obj_type, const NMPCacheId *cache_id);