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.
This commit is contained in:
@@ -1072,6 +1072,24 @@ nmp_cache_id_init_routes_visible (NMPCacheId *id,
|
|||||||
return 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
|
static gboolean
|
||||||
@@ -1095,6 +1113,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
|
static gboolean
|
||||||
_vt_cmd_obj_init_cache_id_ipx_address (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
|
_vt_cmd_obj_init_cache_id_ipx_address (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
|
||||||
{
|
{
|
||||||
@@ -1356,7 +1391,7 @@ nmp_cache_lookup_link_full (const NMPCache *cache,
|
|||||||
const NMPObject *obj;
|
const NMPObject *obj;
|
||||||
const NMPlatformObject *const *list;
|
const NMPlatformObject *const *list;
|
||||||
guint i, len;
|
guint i, len;
|
||||||
NMPCacheId cache_id;
|
NMPCacheId cache_id, *p_cache_id;
|
||||||
|
|
||||||
if (ifindex > 0) {
|
if (ifindex > 0) {
|
||||||
obj = nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&obj_needle, ifindex));
|
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)
|
} else if (!ifname && !match_fn)
|
||||||
return NULL;
|
return NULL;
|
||||||
else {
|
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++) {
|
for (i = 0; i < len; i++) {
|
||||||
obj = NMP_OBJECT_UP_CAST (list[i]);
|
obj = NMP_OBJECT_UP_CAST (list[i]);
|
||||||
|
|
||||||
@@ -1933,6 +1975,7 @@ const NMPClass _nmp_classes[NMP_OBJECT_TYPE_MAX] = {
|
|||||||
.rtm_gettype = RTM_GETLINK,
|
.rtm_gettype = RTM_GETLINK,
|
||||||
.signal_type_id = NM_PLATFORM_SIGNAL_ID_LINK,
|
.signal_type_id = NM_PLATFORM_SIGNAL_ID_LINK,
|
||||||
.signal_type = NM_PLATFORM_SIGNAL_LINK_CHANGED,
|
.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_cmp = _vt_cmd_obj_cmp_link,
|
||||||
.cmd_obj_copy = _vt_cmd_obj_copy_link,
|
.cmd_obj_copy = _vt_cmd_obj_copy_link,
|
||||||
.cmd_obj_stackinit_id = _vt_cmd_obj_stackinit_id_link,
|
.cmd_obj_stackinit_id = _vt_cmd_obj_stackinit_id_link,
|
||||||
|
@@ -64,6 +64,9 @@ typedef enum { /*< skip >*/
|
|||||||
/* all the objects of a certain type */
|
/* all the objects of a certain type */
|
||||||
NMP_CACHE_ID_TYPE_OBJECT_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 */
|
/* all the visible objects of a certain type */
|
||||||
NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY,
|
NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY,
|
||||||
|
|
||||||
@@ -104,6 +107,11 @@ typedef struct {
|
|||||||
guint8 obj_type; /* NMPObjectType as guint8 */
|
guint8 obj_type; /* NMPObjectType as guint8 */
|
||||||
int ifindex;
|
int ifindex;
|
||||||
} object_type_by_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;
|
} 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_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_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_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);
|
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);
|
GArray *nmp_cache_lookup_multi_to_array (const NMPCache *cache, NMPObjectType obj_type, const NMPCacheId *cache_id);
|
||||||
|
Reference in New Issue
Block a user