[NFVBENCH-68] Skip bonding interfaces for interface discovery
[nfvbench.git] / nfvbench / utils.py
index fc72517..cc649bd 100644 (file)
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import errno
-import fcntl
-from functools import wraps
-import json
-from log import LOG
+import glob
 from math import isnan
 import os
 import re
 import signal
 import subprocess
 
+import errno
+import fcntl
+from functools import wraps
+import json
+from log import LOG
+
 
 class TimeoutError(Exception):
     pass
@@ -30,7 +32,7 @@ class TimeoutError(Exception):
 
 def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
     def decorator(func):
-        def _handle_timeout(signum, frame):
+        def _handle_timeout(_signum, _frame):
             raise TimeoutError(error_message)
 
         def wrapper(*args, **kwargs):
@@ -60,7 +62,7 @@ def save_json_result(result, json_file, std_json_path, service_chain, service_ch
 
     if filepaths:
         for file_path in filepaths:
-            LOG.info('Saving results in json file: ' + file_path + "...")
+            LOG.info('Saving results in json file: %s...', file_path)
             with open(file_path, 'w') as jfp:
                 json.dump(result,
                           jfp,
@@ -93,7 +95,8 @@ def dict_to_json_dict(record):
 def get_intel_pci(nic_ports):
     """Returns the first two PCI addresses of sorted PCI list for Intel NIC (i40e, ixgbe)"""
     hx = r'[0-9a-fA-F]'
-    regex = r'{hx}{{4}}:({hx}{{2}}:{hx}{{2}}\.{hx}{{1}}).*(drv={driver}|.*unused=.*{driver})'
+    regex = r'({hx}{{4}}:({hx}{{2}}:{hx}{{2}}\.{hx}{{1}})).*(drv={driver}|.*unused=.*{driver})'
+    pcis = []
 
     try:
         trex_base_dir = '/opt/trex'
@@ -109,14 +112,38 @@ def get_intel_pci(nic_ports):
 
     for driver in ['i40e', 'ixgbe']:
         matches = re.findall(regex.format(hx=hx, driver=driver), devices)
-        if matches:
-            pcis = map(lambda x: x[0], matches)
-            if len(pcis) < 2:
+        if not matches:
+            continue
+
+        matches.sort()
+        if nic_ports:
+            if max(nic_ports) > len(matches) - 1:
+                # If this is hard requirements (i.e. ports are defined
+                # explictly), but there are not enough ports for the
+                # current NIC, just skip the current NIC and looking for
+                # next available one.
                 continue
-            pcis.sort()
-            return [pcis[port_index] for port_index in nic_ports]
-
-    return []
+            else:
+                return [matches[idx][1] for idx in nic_ports]
+        else:
+            for port in matches:
+                intf_name = glob.glob("/sys/bus/pci/devices/%s/net/*" % port[0])
+                if not intf_name:
+                    # Interface is not bind to kernel driver, so take it
+                    pcis.append(port[1])
+                else:
+                    intf_name = intf_name[0][intf_name[0].rfind('/') + 1:]
+                    process = subprocess.Popen(['ip', '-o', '-d', 'link', 'show', intf_name],
+                                               stdout=subprocess.PIPE,
+                                               stderr=subprocess.PIPE)
+                    intf_info, _ = process.communicate()
+                    if not re.search('team_slave|bond_slave', intf_info):
+                        pcis.append(port[1])
+
+                if len(pcis) == 2:
+                    break
+
+    return pcis
 
 
 multiplier_map = {