diff --git a/setup_ovs/helpers.py b/setup_ovs/helpers.py index 5e396ce..2d39215 100644 --- a/setup_ovs/helpers.py +++ b/setup_ovs/helpers.py @@ -1,7 +1,10 @@ # Copyright (C) 2021, RTE (http://www.rte-france.com) # SPDX-License-Identifier: Apache-2.0 +import functools +import os import re +import shutil import subprocess import logging @@ -17,6 +20,24 @@ dry_run = False +@functools.lru_cache(maxsize=None) +def find_command(logical_name, *candidates): + """Locate a system command by trying candidate names/paths.""" + for candidate in candidates: + if os.path.isabs(candidate): + if os.path.isfile(candidate) and os.access(candidate, os.X_OK): + logging.debug("Found %s at %s", logical_name, candidate) + return candidate + else: + resolved = shutil.which(candidate) + if resolved: + logging.debug("Found %s at %s (via PATH)", logical_name, resolved) + return resolved + raise FileNotFoundError( + "Could not find {}: looked for {}".format(logical_name, ", ".join(candidates)) + ) + + def run_command(*cmd_args, **kargs): """ Shell runner helper diff --git a/setup_ovs/ovs.py b/setup_ovs/ovs.py index b140559..959c8c2 100644 --- a/setup_ovs/ovs.py +++ b/setup_ovs/ovs.py @@ -115,15 +115,20 @@ def clear_tap(config): interface, ) +_DPDK_DEVBIND_CANDIDATES = ( + "/usr/bin/dpdk-devbind.py", + "/usr/sbin/dpdk-devbind", +) + + def _bind_dpdk_interfaces(dpdk_interfaces): """ Bind NIC in DPDK """ + dpdk_devbind = helpers.find_command("dpdk-devbind", *_DPDK_DEVBIND_CANDIDATES) for nic in dpdk_interfaces: logging.info("Attach {} to DPDK".format(nic)) - helpers.run_command( - "/usr/sbin/dpdk-devbind", "--force", "--bind=vfio-pci", nic - ) + helpers.run_command(dpdk_devbind, "--force", "--bind=vfio-pci", nic) def _create_bridges(config, dpdk_bridges):