platform: refactor link-type to string conversion

This commit is contained in:
Dan Williams
2015-04-17 10:04:21 +02:00
parent 388b7830f3
commit b484b03acf

View File

@@ -848,43 +848,79 @@ check_support_user_ipv6ll (NMPlatform *platform)
/* Object type specific utilities */ /* Object type specific utilities */
typedef struct {
const NMLinkType nm_type;
const char *type_string;
/* IFLA_INFO_KIND / rtnl_link_get_type() where applicable; the rtnl type
* should only be specified if the device type can be created without
* additional parameters, and if the device type can be determined from
* the rtnl_type. eg, tun/tap should not be specified since both
* tun and tap devices use "tun", and InfiniBand should not be
* specified because a PKey is required at creation. Drivers set this
* value from their 'struct rtnl_link_ops' structure.
*/
const char *rtnl_type;
/* uevent DEVTYPE where applicable, from /sys/class/net/<ifname>/uevent;
* drivers set this value from their SET_NETDEV_DEV() call and the
* 'struct device_type' name member.
*/
const char *devtype;
} LinkDesc;
static const LinkDesc linktypes[] = {
{ NM_LINK_TYPE_NONE, "none", NULL, NULL },
{ NM_LINK_TYPE_UNKNOWN, "unknown", NULL, NULL },
{ NM_LINK_TYPE_ETHERNET, "ethernet", NULL, NULL },
{ NM_LINK_TYPE_INFINIBAND, "infiniband", NULL, NULL },
{ NM_LINK_TYPE_OLPC_MESH, "olpc-mesh", NULL, NULL },
{ NM_LINK_TYPE_WIFI, "wifi", NULL, "wlan" },
{ NM_LINK_TYPE_WWAN_ETHERNET, "wwan", NULL, "wwan" },
{ NM_LINK_TYPE_WIMAX, "wimax", "wimax", "wimax" },
{ NM_LINK_TYPE_DUMMY, "dummy", "dummy", NULL },
{ NM_LINK_TYPE_GRE, "gre", "gre", NULL },
{ NM_LINK_TYPE_GRETAP, "gretap", "gretap", NULL },
{ NM_LINK_TYPE_IFB, "ifb", "ifb", NULL },
{ NM_LINK_TYPE_LOOPBACK, "loopback", NULL, NULL },
{ NM_LINK_TYPE_MACVLAN, "macvlan", "macvlan", NULL },
{ NM_LINK_TYPE_MACVTAP, "macvtap", "macvtap", NULL },
{ NM_LINK_TYPE_OPENVSWITCH, "openvswitch", "openvswitch", NULL },
{ NM_LINK_TYPE_TAP, "tap", NULL, NULL },
{ NM_LINK_TYPE_TUN, "tun", NULL, NULL },
{ NM_LINK_TYPE_VETH, "veth", "veth", NULL },
{ NM_LINK_TYPE_VLAN, "vlan", "vlan", "vlan" },
{ NM_LINK_TYPE_VXLAN, "vxlan", "vxlan", "vxlan" },
{ NM_LINK_TYPE_BRIDGE, "bridge", "bridge", "bridge" },
{ NM_LINK_TYPE_BOND, "bond", "bond", "bond" },
{ NM_LINK_TYPE_TEAM, "team", "team", NULL },
};
static const char * static const char *
type_to_string (NMLinkType type) nm_link_type_to_rtnl_type_string (NMLinkType type)
{ {
/* Note that this only has to support virtual types */ int i;
switch (type) {
case NM_LINK_TYPE_DUMMY: for (i = 0; i < G_N_ELEMENTS (linktypes); i++) {
return "dummy"; if (type == linktypes[i].nm_type)
case NM_LINK_TYPE_GRE: return linktypes[i].rtnl_type;
return "gre";
case NM_LINK_TYPE_GRETAP:
return "gretap";
case NM_LINK_TYPE_IFB:
return "ifb";
case NM_LINK_TYPE_MACVLAN:
return "macvlan";
case NM_LINK_TYPE_MACVTAP:
return "macvtap";
case NM_LINK_TYPE_TAP:
return "tap";
case NM_LINK_TYPE_TUN:
return "tun";
case NM_LINK_TYPE_VETH:
return "veth";
case NM_LINK_TYPE_VLAN:
return "vlan";
case NM_LINK_TYPE_VXLAN:
return "vxlan";
case NM_LINK_TYPE_BRIDGE:
return "bridge";
case NM_LINK_TYPE_BOND:
return "bond";
case NM_LINK_TYPE_TEAM:
return "team";
default:
g_warning ("Wrong type: %d", type);
return NULL;
} }
g_return_val_if_reached (NULL);
}
static const char *
nm_link_type_to_string (NMLinkType type)
{
int i;
for (i = 0; i < G_N_ELEMENTS (linktypes); i++) {
if (type == linktypes[i].nm_type)
return linktypes[i].type_string;
}
g_return_val_if_reached (NULL);
} }
#define DEVTYPE_PREFIX "DEVTYPE=" #define DEVTYPE_PREFIX "DEVTYPE="
@@ -2298,7 +2334,7 @@ build_rtnl_link (int ifindex, const char *name, NMLinkType type)
rtnllink = _nm_rtnl_link_alloc (ifindex, name); rtnllink = _nm_rtnl_link_alloc (ifindex, name);
if (type) { if (type) {
nle = rtnl_link_set_type (rtnllink, type_to_string (type)); nle = rtnl_link_set_type (rtnllink, nm_link_type_to_rtnl_type_string (type));
g_assert (!nle); g_assert (!nle);
} }
return (struct nl_object *) rtnllink; return (struct nl_object *) rtnllink;
@@ -2322,7 +2358,7 @@ link_add (NMPlatform *platform, const char *name, NMLinkType type, const void *a
} }
debug ("link: add link '%s' of type '%s' (%d)", debug ("link: add link '%s' of type '%s' (%d)",
name, type_to_string (type), (int) type); name, nm_link_type_to_string (type), (int) type);
l = build_rtnl_link (0, name, type); l = build_rtnl_link (0, name, type);