Line length: Fix recent merges
[csit.git] / resources / libraries / python / IrqUtil.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """IRQ handling library."""
15
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
20
21
22 class IrqUtil:
23     """Contains methods for managing IRQs."""
24
25     @staticmethod
26     def get_pci_interface_irqs(node, pci_addr):
27         """Get IRQs for interface in linux specified by PCI address.
28
29         :param node: Topology node.
30         :param pci_addr: Linux interface PCI address.
31         :type node: dict
32         :type pci_addr: str
33         :returns: List of IRQs attached to specified interface.
34         :rtype: list
35         """
36         interface = InterfaceUtil.pci_to_eth(node, pci_addr)
37         return IrqUtil.get_interface_irqs(node, interface)
38
39     @staticmethod
40     def get_interface_irqs(node, interface):
41         """Get IRQs for interface in linux.
42
43         :param node: Topology node.
44         :param interface: Linux interface name.
45         :type node: dict
46         :type interface: str
47         :returns: List of IRQs attached to specified interface.
48         :rtype: list
49         """
50         irqs = []
51
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
56         )
57
58         for line in stdout.splitlines():
59             irqs.append(int(line.strip()))
60
61         return irqs
62
63     @staticmethod
64     def set_interface_irqs_affinity(node, interface, cpu_skip_cnt=0, cpu_cnt=1):
65         """Set IRQs affinity for interface in linux.
66
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)
72         :type node: dict
73         :type interface: str
74         :type cpu_skip_cnt: int
75         :type cpu_cnt: int
76         """
77         cpu_list = CpuUtils.get_affinity_af_xdp(
78             node, interface, cpu_skip_cnt=cpu_skip_cnt, cpu_cnt=cpu_cnt
79         )
80         interface = Topology.get_interface_name(node, interface)
81         irq_list = IrqUtil.get_interface_irqs(node, interface)
82
83         for irq, cpu in zip(irq_list, cpu_list):
84             if cpu < 32:
85                 mask = 1 << cpu
86                 mask = f"{mask:x}"
87             else:
88                 groups = int(cpu/32)
89                 mask_fill = u""
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}"
94
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']}!"
97             exec_cmd_no_error(
98                 node, command, timeout=30, sudo=True, message=message
99             )