platform: add nm_platform_link_get_type_name()

Add a platform method to return the name of the link type. (Eg,
"ethernet", "loopback", "tun")
This commit is contained in:
Dan Winship
2013-04-26 11:43:08 -04:00
parent ce4f2a4bd6
commit be5ef71ab0
5 changed files with 105 additions and 21 deletions

View File

@@ -69,6 +69,32 @@ sysctl_get (NMPlatform *platform, const char *path)
return g_strdup (g_hash_table_lookup (priv->options, path));
}
static const char *
type_to_type_name (NMLinkType type)
{
switch (type) {
case NM_LINK_TYPE_UNKNOWN:
return "unknown";
case NM_LINK_TYPE_GENERIC:
return "generic";
case NM_LINK_TYPE_LOOPBACK:
return "loopback";
case NM_LINK_TYPE_ETHERNET:
return "ethernet";
case NM_LINK_TYPE_DUMMY:
return "dummy";
case NM_LINK_TYPE_BRIDGE:
return "bridge";
case NM_LINK_TYPE_BOND:
return "bond";
case NM_LINK_TYPE_TEAM:
return "team";
case NM_LINK_TYPE_NONE:
default:
return NULL;
}
}
static void
link_init (NMPlatformLink *device, int ifindex, int type, const char *name)
{
@@ -78,6 +104,7 @@ link_init (NMPlatformLink *device, int ifindex, int type, const char *name)
device->ifindex = name ? ifindex : 0;
device->type = type;
device->type_name = type_to_type_name (type);
if (name)
strcpy (device->name, name);
switch (device->type) {
@@ -187,6 +214,12 @@ link_get_type (NMPlatform *platform, int ifindex)
return device ? device->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 void
link_changed (NMPlatform *platform, NMPlatformLink *device)
{
@@ -841,6 +874,7 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass)
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_set_up = link_set_up;
platform_class->link_set_down = link_set_down;

View File

@@ -279,35 +279,42 @@ type_to_string (NMLinkType type)
}
}
#define return_type(t, name) \
G_STMT_START { \
if (out_name) \
*out_name = name; \
return t; \
} G_STMT_END
static NMLinkType
link_extract_type (struct rtnl_link *rtnllink)
link_extract_type (struct rtnl_link *rtnllink, const char **out_name)
{
const char *type;
if (!rtnllink)
return NM_LINK_TYPE_NONE;
return_type (NM_LINK_TYPE_NONE, "none");
type = rtnl_link_get_type (rtnllink);
if (!type)
switch (rtnl_link_get_arptype (rtnllink)) {
case ARPHRD_LOOPBACK:
return NM_LINK_TYPE_LOOPBACK;
return_type (NM_LINK_TYPE_LOOPBACK, "loopback");
case ARPHRD_ETHER:
return NM_LINK_TYPE_ETHERNET;
return_type (NM_LINK_TYPE_ETHERNET, "ethernet");
default:
return NM_LINK_TYPE_GENERIC;
return_type (NM_LINK_TYPE_GENERIC, "generic");
}
else if (!g_strcmp0 (type, "dummy"))
return NM_LINK_TYPE_DUMMY;
else if (!g_strcmp0 (type, "bridge"))
return NM_LINK_TYPE_BRIDGE;
else if (!g_strcmp0 (type, "bond"))
return NM_LINK_TYPE_BOND;
else if (!g_strcmp0 (type, "team"))
return NM_LINK_TYPE_TEAM;
else if (!strcmp (type, "dummy"))
return_type (NM_LINK_TYPE_DUMMY, "dummy");
else if (!strcmp (type, "bridge"))
return_type (NM_LINK_TYPE_BRIDGE, "bridge");
else if (!strcmp (type, "bond"))
return_type (NM_LINK_TYPE_BOND, "bond");
else if (!strcmp (type, "team"))
return_type (NM_LINK_TYPE_TEAM, "team");
else
return NM_LINK_TYPE_UNKNOWN;
return_type (NM_LINK_TYPE_UNKNOWN, "unknown");
}
static void
@@ -319,7 +326,7 @@ link_init (NMPlatformLink *info, struct rtnl_link *rtnllink)
info->ifindex = rtnl_link_get_ifindex (rtnllink);
strcpy (info->name, rtnl_link_get_name (rtnllink));
info->type = link_extract_type (rtnllink);
info->type = link_extract_type (rtnllink, &info->type_name);
info->up = !!(rtnl_link_get_flags (rtnllink) & IFF_UP);
info->connected = !!(rtnl_link_get_flags (rtnllink) & IFF_LOWER_UP);
info->arp = !(rtnl_link_get_flags (rtnllink) & IFF_NOARP);
@@ -355,7 +362,7 @@ hack_empty_master_iff_lower_up (NMPlatform *platform, struct nl_object *object)
ifindex = rtnl_link_get_ifindex (rtnllink);
switch (link_extract_type (rtnllink)) {
switch (link_extract_type (rtnllink, NULL)) {
case NM_LINK_TYPE_BRIDGE:
case NM_LINK_TYPE_BOND:
for (slave = nl_cache_get_first (priv->link_cache); slave; slave = nl_cache_get_next (slave)) {
@@ -903,7 +910,17 @@ link_get_type (NMPlatform *platform, int ifindex)
{
auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);
return link_extract_type (rtnllink);
return link_extract_type (rtnllink, NULL);
}
static const char *
link_get_type_name (NMPlatform *platform, int ifindex)
{
auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);
const char *type;
link_extract_type (rtnllink, &type);
return type;
}
static guint32
@@ -1619,6 +1636,7 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
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_set_up = link_set_up;
platform_class->link_set_down = link_set_down;

View File

@@ -424,6 +424,25 @@ nm_platform_link_get_type (int ifindex)
return klass->link_get_type (platform, ifindex);
}
/**
* nm_platform_link_get_type_name:
* @ifindex: Interface index.
*
* Returns: A string describing the type of link. In some cases this
* may be more specific than nm_platform_link_get_type(), but in
* other cases it may not. On error, %NULL is returned.
*/
const char *
nm_platform_link_get_type_name (int ifindex)
{
reset_error ();
g_return_val_if_fail (klass->link_get_type_name, NULL);
return klass->link_get_type_name (platform, ifindex);
}
/**
* nm_platform_link_is_up:
* @ifindex: Interface index

View File

@@ -61,6 +61,7 @@ typedef struct {
int ifindex;
char name[IFNAMSIZ];
NMLinkType type;
const char *type_name;
int master;
gboolean up;
gboolean connected;
@@ -147,6 +148,7 @@ typedef struct {
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_set_up) (NMPlatform *, int ifindex);
gboolean (*link_set_down) (NMPlatform *, int ifindex);
@@ -243,6 +245,7 @@ gboolean nm_platform_link_delete_by_name (const char *ifindex);
int nm_platform_link_get_ifindex (const char *name);
const char *nm_platform_link_get_name (int ifindex);
NMLinkType nm_platform_link_get_type (int ifindex);
const char *nm_platform_link_get_type_name (int ifindex);
gboolean nm_platform_link_set_up (int ifindex);
gboolean nm_platform_link_set_down (int ifindex);

View File

@@ -2,8 +2,10 @@
#define LO_INDEX 1
#define LO_NAME "lo"
#define LO_TYPEDESC "loopback"
#define DEVICE_NAME "nm-test-device"
#define DUMMY_TYPEDESC "dummy"
#define BOGUS_NAME "nm-bogus-device"
#define BOGUS_IFINDEX INT_MAX
#define SLAVE_NAME "nm-test-slave"
@@ -65,6 +67,8 @@ test_bogus(void)
error (NM_PLATFORM_ERROR_NOT_FOUND);
g_assert (!nm_platform_link_get_type (BOGUS_IFINDEX));
error (NM_PLATFORM_ERROR_NOT_FOUND);
g_assert (!nm_platform_link_get_type_name (BOGUS_IFINDEX));
error (NM_PLATFORM_ERROR_NOT_FOUND);
g_assert (!nm_platform_link_set_up (BOGUS_IFINDEX));
error (NM_PLATFORM_ERROR_NOT_FOUND);
@@ -94,6 +98,7 @@ test_loopback (void)
g_assert_cmpint (nm_platform_link_get_type (LO_INDEX), ==, NM_LINK_TYPE_LOOPBACK);
g_assert_cmpint (nm_platform_link_get_ifindex (LO_NAME), ==, LO_INDEX);
g_assert_cmpstr (nm_platform_link_get_name (LO_INDEX), ==, LO_NAME);
g_assert_cmpstr (nm_platform_link_get_type_name (LO_INDEX), ==, LO_TYPEDESC);
g_assert (nm_platform_link_supports_carrier_detect (LO_INDEX));
g_assert (!nm_platform_link_supports_vlans (LO_INDEX));
@@ -225,7 +230,7 @@ test_slave (int master, int type, SignalData *link_added, SignalData *master_cha
}
static void
test_virtual (NMLinkType link_type)
test_virtual (NMLinkType link_type, const char *link_typedesc)
{
int ifindex;
char *value;
@@ -241,6 +246,7 @@ test_virtual (NMLinkType link_type)
ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
g_assert (ifindex >= 0);
g_assert_cmpint (nm_platform_link_get_type (ifindex), ==, link_type);
g_assert_cmpstr (nm_platform_link_get_type_name (ifindex), ==, link_typedesc);
accept_signal (link_added);
/* Add again */
@@ -298,6 +304,8 @@ test_virtual (NMLinkType link_type)
g_assert (!nm_platform_link_exists (DEVICE_NAME)); no_error ();
g_assert_cmpint (nm_platform_link_get_type (ifindex), ==, NM_LINK_TYPE_NONE);
error (NM_PLATFORM_ERROR_NOT_FOUND);
g_assert (!nm_platform_link_get_type (ifindex));
error (NM_PLATFORM_ERROR_NOT_FOUND);
accept_signal (link_removed);
/* Delete again */
@@ -313,19 +321,19 @@ test_virtual (NMLinkType link_type)
static void
test_bridge (void)
{
test_virtual (NM_LINK_TYPE_BRIDGE);
test_virtual (NM_LINK_TYPE_BRIDGE, "bridge");
}
static void
test_bond (void)
{
test_virtual (NM_LINK_TYPE_BOND);
test_virtual (NM_LINK_TYPE_BOND, "bond");
}
static void
test_team (void)
{
test_virtual (NM_LINK_TYPE_TEAM);
test_virtual (NM_LINK_TYPE_TEAM, "team");
}
static void
@@ -355,6 +363,7 @@ test_internal (void)
g_assert (ifindex > 0);
g_assert_cmpstr (nm_platform_link_get_name (ifindex), ==, DEVICE_NAME);
g_assert_cmpint (nm_platform_link_get_type (ifindex), ==, NM_LINK_TYPE_DUMMY);
g_assert_cmpstr (nm_platform_link_get_type_name (ifindex), ==, DUMMY_TYPEDESC);
/* Up/connected */
g_assert (!nm_platform_link_is_up (ifindex)); no_error ();
@@ -424,6 +433,7 @@ test_external (void)
g_assert (ifindex > 0);
g_assert_cmpstr (nm_platform_link_get_name (ifindex), ==, DEVICE_NAME);
g_assert_cmpint (nm_platform_link_get_type (ifindex), ==, NM_LINK_TYPE_DUMMY);
g_assert_cmpstr (nm_platform_link_get_type_name (ifindex), ==, DUMMY_TYPEDESC);
/* Up/connected/arp */
g_assert (!nm_platform_link_is_up (ifindex));