diff options
Diffstat (limited to 'mpm')
-rw-r--r-- | mpm/python/usrp_mpm/sys_utils/net.py | 46 |
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): """ |