# License for the specific language governing permissions and limitations
# under the License.
+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
-import os
-import re
-import signal
-import subprocess
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):
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,
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'
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 = {
return flow_count * multiplier
+def cast_integer(value):
+ return int(value) if not isnan(value) else value
+
+
class RunLock(object):
"""
Attempts to lock file and run current instance of NFVbench as the first,