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:
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 """QAT util library."""
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
23 """Contains methods for setting up QATs."""
26 def crypto_device_verify_on_all_duts(nodes):
27 """Verify if Crypto QAT device and its virtual functions are initialized
30 :param nodes: Nodes in the topology.
33 VPPUtil.stop_vpp_service_on_all_duts(nodes)
35 for node in nodes.values():
36 if node["type"] == NodeType.DUT:
37 cryptodevs = Topology.get_cryptodev(node)
40 for device in cryptodevs.values():
41 QATUtil.crypto_device_init(node, device)
44 def crypto_device_init(node, device):
45 """Init Crypto QAT device virtual functions on DUT.
47 :param node: DUT node.
48 :device: Crypto device entry from topology file.
52 DUTSetup.verify_kernel_module(node, device["module"], force_load=True)
54 current_driver = DUTSetup.get_pci_dev_driver(
55 node, device["pci_address"].replace(":", r"\:")
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"])
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!"
68 qat_dev = stdout.split("_")[-1]
69 conf_file = f"/etc/{device['driver']}_{qat_dev.strip()}.conf"
71 node, f"adf_ctl --config {conf_file} {stdout.strip()} restart",
72 sudo=True, message="Failed to restart crypto device!"
75 raise ValueError("Crypto device error")
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"]
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
90 DUTSetup.pci_vf_driver_bind(
91 node, device["pci_address"], cvf, "vfio-pci"