From 0bf443acca2985a10ef26c1651992c185d4fd4fa Mon Sep 17 00:00:00 2001 From: zsdc Date: Tue, 27 Jun 2023 23:04:14 +0300 Subject: VPP: T1797: Improved PCI address search Use info from both ethtool and VPP to find PCI address for an interface. --- python/vyos/vpp.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'python/vyos') diff --git a/python/vyos/vpp.py b/python/vyos/vpp.py index 9e9471879..d60ecc1b3 100644 --- a/python/vyos/vpp.py +++ b/python/vyos/vpp.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see . +from re import search as re_search, MULTILINE as re_M + from vpp_papi import VPPApiClient @@ -27,17 +29,29 @@ class VPPControl: self.vpp_api_client.connect('vpp-vyos') def __del__(self) -> None: + """Disconnect from VPP API (destructor) + """ + self.disconnect() + + def disconnect(self) -> None: """Disconnect from VPP API """ self.vpp_api_client.disconnect() - def cli_cmd(self, command: str) -> None: + def cli_cmd(self, command: str, return_output: bool = False) -> str: """Send raw CLI command Args: command (str): command to send + return_output (bool, optional): Return command output. Defaults to False. + + Returns: + str: output of the command, only if it was successful """ - self.vpp_api_client.api.cli_inband(cmd=command) + cli_answer = self.vpp_api_client.api.cli_inband(cmd=command) + if return_output and cli_answer.retval == 0: + return cli_answer.reply + return '' def get_mac(self, ifname: str) -> str: """Find MAC address by interface name in VPP @@ -112,3 +126,25 @@ class VPPControl: iface_index = self.get_sw_if_index(iface_name) self.vpp_api_client.api.sw_interface_set_rx_mode( sw_if_index=iface_index, mode=modes_dict[rx_mode]) + + def get_pci_addr(self, ifname: str) -> str: + """Find PCI address of interface by interface name in VPP + + Args: + ifname (str): interface name inside VPP + + Returns: + str: PCI address + """ + hw_info = self.cli_cmd(f'show hardware-interfaces {ifname}', + return_output=True) + + regex_filter = r'^\s+pci: device (?P\w+:\w+) subsystem (?P\w+:\w+) address (?P
\w+:\w+:\w+\.\w+) numa (?P\w+)$' + re_obj = re_search(regex_filter, hw_info, re_M) + + # return empty string if no interface or no PCI info was found + if not hw_info or not re_obj: + return '' + + address = re_obj.groupdict().get('address', '') + return address -- cgit v1.2.3