1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """IRQ handling library."""
16 from resources.libraries.python.CpuUtils import CpuUtils
17 from resources.libraries.python.InterfaceUtil import InterfaceUtil
18 from resources.libraries.python.ssh import exec_cmd_no_error
19 from resources.libraries.python.topology import Topology
23 """Contains methods for managing IRQs."""
26 def get_pci_interface_irqs(node, pci_addr):
27 """Get IRQs for interface in linux specified by PCI address.
29 :param node: Topology node.
30 :param pci_addr: Linux interface PCI address.
33 :returns: List of IRQs attached to specified interface.
36 interface = InterfaceUtil.pci_to_eth(node, pci_addr)
37 return IrqUtil.get_interface_irqs(node, interface)
40 def get_interface_irqs(node, interface):
41 """Get IRQs for interface in linux.
43 :param node: Topology node.
44 :param interface: Linux interface name.
47 :returns: List of IRQs attached to specified interface.
52 command = f"grep '{interface}-.*TxRx' /proc/interrupts | cut -f1 -d:"
53 message = f"Failed to get IRQs for {interface} on {node['host']}!"
54 stdout, _ = exec_cmd_no_error(
55 node, command, timeout=30, sudo=True, message=message
58 for line in stdout.splitlines():
59 irqs.append(int(line.strip()))
64 def set_interface_irqs_affinity(node, interface, cpu_skip_cnt=0, cpu_cnt=1):
65 """Set IRQs affinity for interface in linux.
67 :param node: Topology node.
68 :param interface: Topology interface.
69 :param cpu_skip_cnt: Amount of CPU cores to skip.
70 :param cpu_cnt: CPU threads count. (Optional, Default: 0)
71 :param cpu_list: List of CPUs. (Optional, Default: 1)
74 :type cpu_skip_cnt: int
77 cpu_list = CpuUtils.get_affinity_af_xdp(
78 node, interface, cpu_skip_cnt=cpu_skip_cnt, cpu_cnt=cpu_cnt
80 interface = Topology.get_interface_name(node, interface)
81 irq_list = IrqUtil.get_interface_irqs(node, interface)
83 for irq, cpu in zip(irq_list, cpu_list):
90 for _ in range(groups):
91 mask_fill = f"{mask_fill},00000000"
92 mask = 1 << (cpu - (32 * groups))
93 mask = f"{mask:x}{mask_fill}"
95 command = f"sh -c 'echo {mask} > /proc/irq/{irq}/smp_affinity'"
96 message = f"Failed to set IRQ affinity for {irq} on {node['host']}!"
98 node, command, timeout=30, sudo=True, message=message