diff options
author | Martin Braun <martin.braun@ettus.com> | 2018-06-08 14:01:54 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2018-06-08 14:58:20 -0700 |
commit | 8762a4d444e0ba68b133245e4984763a9521037a (patch) | |
tree | 6f8eeec27462c546128c3dde9803002ea16c4c07 /mpm/python | |
parent | 22d684c3c8db8644cbdaf7b2955048050007ec30 (diff) | |
download | uhd-8762a4d444e0ba68b133245e4984763a9521037a.tar.gz uhd-8762a4d444e0ba68b133245e4984763a9521037a.tar.bz2 uhd-8762a4d444e0ba68b133245e4984763a9521037a.zip |
mpm: net: Fix rare failures for IP addr detection
Because the detection of valid Ethernet devices happens across multiple
calls in a non-atomic fashion, we cannot assume that a device passed to
net.get_iface_info() actually has a valid IP address, so we don't make
that assumption.
Diffstat (limited to 'mpm/python')
-rw-r--r-- | mpm/python/usrp_mpm/sys_utils/net.py | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/mpm/python/usrp_mpm/sys_utils/net.py b/mpm/python/usrp_mpm/sys_utils/net.py index ccce0762e..95dc14ed8 100644 --- a/mpm/python/usrp_mpm/sys_utils/net.py +++ b/mpm/python/usrp_mpm/sys_utils/net.py @@ -41,8 +41,8 @@ def get_iface_info(ifname): """ Given an interface name (e.g. 'eth1'), return a dictionary with the following keys: - - ip_addr: Main IPv4 address - - ip_addrs: List of valid IPv4 addresses + - ip_addr: Main IPv4 address, if set, or an empty string otherwise. + - ip_addrs: List of valid IPv4 addresses. Can be an empty list. - mac_addr: MAC address All values are stored as strings. @@ -61,7 +61,7 @@ def get_iface_info(ifname): ip_addrs = get_iface_addrs(mac_addr) return { 'mac_addr': mac_addr, - 'ip_addr': ip_addrs[0], + 'ip_addr': ip_addrs[0] if ip_addrs else '', 'ip_addrs': ip_addrs, } @@ -83,20 +83,21 @@ def ip_addr_to_iface(ip_addr, iface_list): def get_iface_addrs(mac_addr): """ - return ipv4 addresses for a given macaddress - input format: "aa:bb:cc:dd:ee:ff" + Return a list of IPv4 addresses for a given MAC address. + If there are no IP addresses assigned to the MAC address, it will return + an empty list. + + Arguments: + mac_addr -- A MAC address as a string, input format: "aa:bb:cc:dd:ee:ff" """ - with IPRoute() as ip2: - # returns index - [link] = ip2.link_lookup(address=mac_addr) + with IPRoute() as ip2: + [link_index] = ip2.link_lookup(address=mac_addr) # Only get v4 addresses addresses = [addr.get_attrs('IFA_ADDRESS') for addr in ip2.get_addr(family=socket.AF_INET) - if addr.get('index', None) == link] + if addr.get('index', None) == link_index] # flatten possibly nested list - addresses = list(itertools.chain.from_iterable(addresses)) - - return addresses + return list(itertools.chain.from_iterable(addresses)) def byte_to_mac(byte_str): |