style(PLRsearch): format according to black
[csit.git] / resources / libraries / python / QATUtil.py
1 # Copyright (c) 2023 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 """QAT util library."""
15
16 from resources.libraries.python.DUTSetup import DUTSetup
17 from resources.libraries.python.topology import NodeType, Topology
18 from resources.libraries.python.VPPUtil import VPPUtil
19 from resources.libraries.python.ssh import exec_cmd_no_error
20
21
22 class QATUtil:
23     """Contains methods for setting up QATs."""
24
25     @staticmethod
26     def crypto_device_verify_on_all_duts(nodes):
27         """Verify if Crypto QAT device and its virtual functions are initialized
28         on all DUTs.
29
30         :param nodes: Nodes in the topology.
31         :type nodes: dict
32         """
33         VPPUtil.stop_vpp_service_on_all_duts(nodes)
34
35         for node in nodes.values():
36             if node["type"] == NodeType.DUT:
37                 cryptodevs = Topology.get_cryptodev(node)
38                 if not cryptodevs:
39                     return
40                 for device in cryptodevs.values():
41                     QATUtil.crypto_device_init(node, device)
42
43     @staticmethod
44     def crypto_device_init(node, device):
45         """Init Crypto QAT device virtual functions on DUT.
46
47         :param node: DUT node.
48         :device: Crypto device entry from topology file.
49         :type node: dict
50         :type device: dict
51         """
52         DUTSetup.verify_kernel_module(node, device["module"], force_load=True)
53
54         current_driver = DUTSetup.get_pci_dev_driver(
55             node, device["pci_address"].replace(":", r"\:")
56         )
57         if current_driver is not None:
58             DUTSetup.pci_driver_unbind(node, device["pci_address"])
59         # Bind to kernel driver.
60         DUTSetup.pci_driver_bind(node, device["pci_address"], device["driver"])
61
62         cmd = f"adf_ctl status | grep {device['pci_address']} | "
63         cmd += "awk '{print $1}'"
64         stdout, _ = exec_cmd_no_error(
65             node, cmd, sudo=True, message="Failed to check crypto device!"
66         )
67         if stdout.strip():
68             qat_dev = stdout.split("_")[-1]
69             conf_file = f"/etc/{device['driver']}_{qat_dev.strip()}.conf"
70             exec_cmd_no_error(
71                 node, f"adf_ctl --config {conf_file} {stdout.strip()} restart",
72                 sudo=True, message="Failed to restart crypto device!"
73             )
74         else:
75             raise ValueError("Crypto device error")
76
77         # Initialize QAT VFs.
78         if int(device["numvfs"]) > 0:
79             path = f"drivers/{device['driver']}"
80             DUTSetup.set_sriov_numvfs(
81                 node, device["pci_address"], path=path,
82                 numvfs=device["numvfs"]
83             )
84
85         if device["driver"] not in ["c4xxx"]:
86             for cvf in range(int(device["numvfs"])):
87                 DUTSetup.pci_vf_driver_unbind(
88                     node, device["pci_address"], cvf
89                 )
90                 DUTSetup.pci_vf_driver_bind(
91                     node, device["pci_address"], cvf, "vfio-pci"
92                 )