aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSugandha Gupta <sugandha.gupta@ettus.com>2018-07-25 18:04:51 -0700
committerBrent Stapleton <brent.stapleton@ettus.com>2019-01-17 15:38:23 -0800
commitc81b4ac99f534f835166bc814839757e37ae13cb (patch)
treedbca05e40c087590517c3c216900ec7ae5c77e6f
parent3eb11a606d834f3bdf8d9e0937e5a8f8468822c4 (diff)
downloaduhd-c81b4ac99f534f835166bc814839757e37ae13cb.tar.gz
uhd-c81b4ac99f534f835166bc814839757e37ae13cb.tar.bz2
uhd-c81b4ac99f534f835166bc814839757e37ae13cb.zip
mpm: net: Add bridge utilities
-rw-r--r--mpm/python/usrp_mpm/sys_utils/net.py46
1 files changed, 36 insertions, 10 deletions
diff --git a/mpm/python/usrp_mpm/sys_utils/net.py b/mpm/python/usrp_mpm/sys_utils/net.py
index e28c8bda1..30ecae70d 100644
--- a/mpm/python/usrp_mpm/sys_utils/net.py
+++ b/mpm/python/usrp_mpm/sys_utils/net.py
@@ -48,6 +48,10 @@ def get_iface_info(ifname):
All values are stored as strings.
"""
+ def is_bridge(link_info_info):
+ " Returns True if link_info_ is a bridge "
+ return (link_info_info is not None) and \
+ (link_info_info.get_attr('IFLA_INFO_KIND') == 'bridge')
try:
with IPRoute() as ipr:
links = ipr.link_lookup(ifname=ifname)
@@ -65,7 +69,8 @@ def get_iface_info(ifname):
'mac_addr': mac_addr,
'ip_addr': ip_addrs[0] if ip_addrs else '',
'ip_addrs': ip_addrs,
- 'link_speed': link_speed
+ 'link_speed': link_speed,
+ 'bridge': is_bridge(link_info.get_attr('IFLA_LINKINFO')),
}
@@ -128,20 +133,41 @@ def byte_to_mac(byte_str):
return ':'.join(["%02x" % ord(x) for x in byte_str])
-def get_mac_addr(remote_addr):
+def get_mac_addr(ip_addr):
"""
return MAC address of a remote host already discovered
or None if no host entry was found
"""
with IPRoute() as ip2:
- addrs = ip2.get_neighbours(dst=remote_addr)
- if len(addrs) > 1:
- get_logger('get_mac_addr').warning("More than one device with the "
- "same IP address found. "
- "Picking entry at random")
- if not addrs:
- return None
- return addrs[0].get_attr('NDA_LLADDR')
+ def _get_local_mac_addr(ip_addr):
+ " Lookup MAC addr of local device "
+ if_addr = ip2.get_addr(
+ match=lambda x: x.get_attr('IFA_ADDRESS') == ip_addr
+ )
+ if len(if_addr) == 0:
+ return None
+ if len(if_addr) > 1:
+ get_logger('get_mac_addr').warning(
+ "More than one device with the same IP address `{}' found. "
+ "Picking entry at random.".format(ip_addr)
+ )
+ iface_idx = if_addr[0]['index']
+ if_info = ip2.get_links(iface_idx)[0]
+ return if_info.get_attr('IFLA_ADDRESS')
+ def _get_remote_mac_addr(remote_addr):
+ " Basically an ARP lookup "
+ addrs = ip2.get_neighbours(dst=remote_addr)
+ if len(addrs) > 1:
+ get_logger('get_mac_addr').warning(
+ "More than one device with the same IP address `{}' found. "
+ "Picking entry at random.".format(ip_addr)
+ )
+ if not addrs:
+ return None
+ return addrs[0].get_attr('NDA_LLADDR')
+ mac_addr = _get_local_mac_addr(ip_addr) or _get_remote_mac_addr(ip_addr)
+ ip2.close()
+ return mac_addr
def get_local_ip_addrs(ipv4_only=False):
"""