From b905e0d803e55deee9620a24de4b87c3429b43ab Mon Sep 17 00:00:00 2001 From: Bob Fournier Date: Tue, 4 Apr 2017 13:58:43 -0400 Subject: os_net_config should map nics that are down if nic is in mapping file Currently os-net_config will map nics from a user-supplied mapping file only if the nic is active (operstate = up). This can cause problems if a nic is in a bond and one of the bond's nics has no carrier. This fix will map the nic from the mapping file if the nic is defined on the system, regardless of the operstate status. The fix implements a new function to return a list of available nics (no check of operstate) for use if a mapping file is supplied. The list of active nics must still be used in the default case when numbering nics (no mapping file supplied). There is also some cleanup to check if a user-supplied mac is in the mapping file before attempting to convert the mac to a nic. Change-Id: Ia5d8c8b49b7ac0b51ee42a754f06e5e53587a5f6 Closes-Bug: 1679787 --- os_net_config/utils.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'os_net_config/utils.py') diff --git a/os_net_config/utils.py b/os_net_config/utils.py index 98bfe99..84e719e 100644 --- a/os_net_config/utils.py +++ b/os_net_config/utils.py @@ -92,20 +92,30 @@ def interface_mac(name): def _is_active_nic(interface_name): + return _is_available_nic(interface_name, True) + + +def _is_available_nic(interface_name, check_active=True): try: if interface_name == 'lo': return False device_dir = _SYS_CLASS_NET + '/%s/device' % interface_name has_device_dir = os.path.isdir(device_dir) + if not has_device_dir: + return False operstate = None with open(_SYS_CLASS_NET + '/%s/operstate' % interface_name, 'r') as f: operstate = f.read().rstrip().lower() + if check_active and operstate != 'up': + return False address = None with open(_SYS_CLASS_NET + '/%s/address' % interface_name, 'r') as f: address = f.read().rstrip() + if not address: + return False # If SR-IOV Virtual Functions (VF) are enabled in an interface, there # will be additional nics created for each VF. It has to be ignored in @@ -114,12 +124,12 @@ def _is_active_nic(interface_name): # ignored. vf_path_check = _SYS_CLASS_NET + '/%s/device/physfn' % interface_name is_sriov_vf = os.path.isdir(vf_path_check) - - if (has_device_dir and operstate == 'up' and address and - not is_sriov_vf): - return True - else: + if is_sriov_vf: return False + + # nic is available + return True + except IOError: return False @@ -136,13 +146,21 @@ def _is_embedded_nic(nic): return False +def ordered_available_nics(): + return _ordered_nics(False) + + def ordered_active_nics(): + return _ordered_nics(True) + + +def _ordered_nics(check_active): embedded_nics = [] nics = [] logger.debug("Finding active nics") for name in glob.iglob(_SYS_CLASS_NET + '/*'): nic = name[(len(_SYS_CLASS_NET) + 1):] - if _is_active_nic(nic): + if _is_available_nic(nic, check_active): if _is_embedded_nic(nic): logger.debug("%s is an embedded active nic" % nic) embedded_nics.append(nic) -- cgit 1.2.3-korg