platform: refactor nm_platform_veth_get_properties()

For recent kernels, the peer-ifindex of veths is reported as
parent (IFA_LINK). Prefer that over the ethtool lookup.

For one, this avoids the extra ethtool call which has the
downside of sidestepping the platform cache. Also, looking
up the peer-ifindex in ethtool does not report whether the
peer lifes in another netns (NM_PLATFORM_LINK_OTHER_NETNS).

Only use ethtool as fallback for older kernels.
This commit is contained in:
Thomas Haller
2015-10-16 17:53:33 +02:00
parent dd1f4d8947
commit 7cdbc393a0
6 changed files with 28 additions and 60 deletions

View File

@@ -76,17 +76,18 @@ get_peer (NMDeviceVeth *self)
{
NMDeviceVethPrivate *priv = NM_DEVICE_VETH_GET_PRIVATE (self);
NMDevice *device = NM_DEVICE (self), *peer = NULL;
NMPlatformVethProperties props;
int peer_ifindex;
if (priv->ever_had_peer)
return priv->peer;
if (!nm_platform_veth_get_properties (NM_PLATFORM_GET, nm_device_get_ifindex (device), &props)) {
if (!nm_platform_veth_get_properties (NM_PLATFORM_GET, nm_device_get_ifindex (device), &peer_ifindex)) {
_LOGW (LOGD_HW, "could not read veth properties");
return NULL;
}
peer = nm_manager_get_device_by_ifindex (nm_manager_get (), props.peer);
if (peer_ifindex > 0)
peer = nm_manager_get_device_by_ifindex (nm_manager_get (), peer_ifindex);
if (peer && NM_IS_DEVICE_VETH (peer)) {
set_peer (self, peer);
set_peer (NM_DEVICE_VETH (peer), device);

View File

@@ -701,12 +701,6 @@ infiniband_partition_add (NMPlatform *platform, int parent, int p_key, NMPlatfor
return TRUE;
}
static gboolean
veth_get_properties (NMPlatform *platform, int ifindex, NMPlatformVethProperties *props)
{
return FALSE;
}
static gboolean
wifi_get_capabilities (NMPlatform *platform, int ifindex, NMDeviceWifiCapabilities *caps)
{
@@ -1438,8 +1432,6 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass)
platform_class->infiniband_partition_add = infiniband_partition_add;
platform_class->veth_get_properties = veth_get_properties;
platform_class->wifi_get_capabilities = wifi_get_capabilities;
platform_class->wifi_get_bssid = wifi_get_bssid;
platform_class->wifi_get_ssid = wifi_get_ssid;

View File

@@ -3976,26 +3976,6 @@ infiniband_partition_add (NMPlatform *platform, int parent, int p_key, NMPlatfor
/******************************************************************/
static gboolean
veth_get_properties (NMPlatform *platform, int ifindex, NMPlatformVethProperties *props)
{
const char *ifname;
int peer_ifindex;
ifname = nm_platform_link_get_name (platform, ifindex);
if (!ifname)
return FALSE;
peer_ifindex = nmp_utils_ethtool_get_peer_ifindex (ifname);
if (peer_ifindex <= 0)
return FALSE;
props->peer = peer_ifindex;
return TRUE;
}
/******************************************************************/
static WifiData *
wifi_get_wifi_data (NMPlatform *platform, int ifindex)
{
@@ -5223,8 +5203,6 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
platform_class->infiniband_partition_add = infiniband_partition_add;
platform_class->veth_get_properties = veth_get_properties;
platform_class->wifi_get_capabilities = wifi_get_capabilities;
platform_class->wifi_get_bssid = wifi_get_bssid;
platform_class->wifi_get_frequency = wifi_get_frequency;

View File

@@ -1688,14 +1688,33 @@ nm_platform_infiniband_get_info (NMPlatform *self,
}
gboolean
nm_platform_veth_get_properties (NMPlatform *self, int ifindex, NMPlatformVethProperties *props)
nm_platform_veth_get_properties (NMPlatform *self, int ifindex, int *out_peer_ifindex)
{
const NMPlatformLink *plink;
int peer_ifindex;
_CHECK_SELF (self, klass, FALSE);
g_return_val_if_fail (ifindex > 0, FALSE);
g_return_val_if_fail (props != NULL, FALSE);
plink = nm_platform_link_get (self, ifindex);
return klass->veth_get_properties (self, ifindex, props);
if (!plink)
return FALSE;
if (plink->type != NM_LINK_TYPE_VETH)
return FALSE;
if (plink->parent != 0) {
NM_SET_OUT (out_peer_ifindex, plink->parent);
return TRUE;
}
/* Pre-4.1 kernel did not expose the peer_ifindex as IFA_LINK. Lookup via ethtool. */
if (out_peer_ifindex) {
peer_ifindex = nmp_utils_ethtool_get_peer_ifindex (plink->name);
if (peer_ifindex <= 0)
return FALSE;
*out_peer_ifindex = peer_ifindex;
}
return TRUE;
}
gboolean

View File

@@ -350,10 +350,6 @@ typedef struct {
guint16 id;
} NMPlatformLnkVlan;
typedef struct {
int peer;
} NMPlatformVethProperties;
typedef struct {
gint64 owner;
gint64 group;
@@ -504,8 +500,6 @@ typedef struct {
gboolean (*infiniband_partition_add) (NMPlatform *, int parent, int p_key, NMPlatformLink *out_link);
gboolean (*veth_get_properties) (NMPlatform *, int ifindex, NMPlatformVethProperties *properties);
gboolean (*wifi_get_capabilities) (NMPlatform *, int ifindex, NMDeviceWifiCapabilities *caps);
gboolean (*wifi_get_bssid) (NMPlatform *, int ifindex, guint8 *bssid);
GByteArray *(*wifi_get_ssid) (NMPlatform *, int ifindex);
@@ -701,7 +695,7 @@ gboolean nm_platform_vlan_set_egress_map (NMPlatform *self, int ifindex, int fro
NMPlatformError nm_platform_infiniband_partition_add (NMPlatform *self, int parent, int p_key, NMPlatformLink *out_link);
gboolean nm_platform_infiniband_get_info (NMPlatform *self, int ifindex, int *parent, int *p_key, const char **mode);
gboolean nm_platform_veth_get_properties (NMPlatform *self, int ifindex, NMPlatformVethProperties *properties);
gboolean nm_platform_veth_get_properties (NMPlatform *self, int ifindex, int *out_peer_ifindex);
gboolean nm_platform_tun_get_properties (NMPlatform *self, int ifindex, NMPlatformTunProperties *properties);
gboolean nm_platform_tun_get_properties_ifname (NMPlatform *platform, const char *ifname, NMPlatformTunProperties *props);

View File

@@ -356,20 +356,6 @@ do_vlan_set_egress_map (char **argv)
return nm_platform_vlan_set_egress_map (NM_PLATFORM_GET, ifindex, from, to);
}
static gboolean
do_veth_get_properties (char **argv)
{
int ifindex = parse_ifindex (*argv++);
NMPlatformVethProperties props;
if (!nm_platform_veth_get_properties (NM_PLATFORM_GET, ifindex, &props))
return FALSE;
printf ("peer: %d\n", props.peer);
return TRUE;
}
static gboolean
do_tun_get_properties (char **argv)
{
@@ -825,8 +811,6 @@ static const command_t commands[] = {
"<ifname/ifindex> <from> <to>" },
{ "vlan-set-egress-map", "set vlan egress map", do_vlan_set_egress_map, 3,
"<ifname/ifindex> <from> <to>" },
{ "veth-get-properties", "get veth properties", do_veth_get_properties, 1,
"<ifname/ifindex>" },
{ "tun-get-properties", "get tun/tap properties", do_tun_get_properties, 1,
"<ifname/ifindex>" },
{ "macvlan-get-properties", "get macvlan properties", do_macvlan_get_properties, 1,