|
1 | | - |
2 | | -# pcieutil.py |
3 | | -# Platform-specific PCIE status interface for SONIC |
4 | | -# |
5 | | - |
6 | | -import os |
7 | | -import yaml |
8 | | -import subprocess |
9 | | -import re |
10 | | -import sys |
11 | | -from copy import deepcopy |
12 | | -try: |
13 | | - from .pcie_base import PcieBase |
14 | | -except ImportError as e: |
15 | | - raise ImportError(str(e) + "- required module not found") |
16 | | - |
17 | | - |
18 | | - |
19 | | -class PcieUtil(PcieBase): |
20 | | - """Platform-specific PCIEutil class""" |
21 | | - # got the config file path |
22 | | - def __init__(self, path): |
23 | | - self.config_path = path |
24 | | - |
25 | | - # load the config file |
26 | | - def load_config_file(self): |
27 | | - config_file = self.config_path + "/" + "pcie.yaml" |
28 | | - try: |
29 | | - with open(config_file) as conf_file: |
30 | | - self.confInfo = yaml.load(conf_file) |
31 | | - except IOError as e: |
32 | | - print "Error: {}".format(str(e)) |
33 | | - print "Not found config file, please add a config file manually, or generate it by running [pcieutil pcie_generate]" |
34 | | - sys.exit() |
35 | | - |
36 | | - # load current PCIe device |
37 | | - def get_pcie_device(self): |
38 | | - pciDict = {} |
39 | | - pciList = [] |
40 | | - p1 = "^(\w+):(\w+)\.(\w)\s(.*)\s\(.*\)" |
41 | | - p2 = "^.*:.*:.*:(\w+)\s\(.*\)" |
42 | | - command1 = "sudo lspci" |
43 | | - command2 = "sudo lspci -n" |
44 | | - # run command 1 |
45 | | - proc1 = subprocess.Popen(command1, shell=True, stdout=subprocess.PIPE) |
46 | | - output1 = proc1.stdout.readlines() |
47 | | - (out, err) = proc1.communicate() |
48 | | - # run command 2 |
49 | | - proc2 = subprocess.Popen(command2, shell=True, stdout=subprocess.PIPE) |
50 | | - output2 = proc2.stdout.readlines() |
51 | | - (out, err) = proc2.communicate() |
52 | | - |
53 | | - if proc1.returncode > 0: |
54 | | - for line1 in output1: |
55 | | - print(line1.strip()) |
56 | | - return |
57 | | - elif proc2.returncode > 0: |
58 | | - for line2 in output2: |
59 | | - print(line2.strip()) |
60 | | - return |
61 | | - else: |
62 | | - for (line1, line2) in zip(output1, output2): |
63 | | - pciDict.clear() |
64 | | - match1 = re.search(p1, line1.strip()) |
65 | | - match2 = re.search(p2, line2.strip()) |
66 | | - if match1 and match2: |
67 | | - Bus = match1.group(1) |
68 | | - Dev = match1.group(2) |
69 | | - Fn = match1.group(3) |
70 | | - Name = match1.group(4) |
71 | | - Id = match2.group(1) |
72 | | - pciDict["name"] = Name |
73 | | - pciDict["bus"] = Bus |
74 | | - pciDict["dev"] = Dev |
75 | | - pciDict["fn"] = Fn |
76 | | - pciDict["id"] = Id |
77 | | - pciList.append(pciDict) |
78 | | - pciDict = deepcopy(pciDict) |
79 | | - else: |
80 | | - print("CAN NOT MATCH PCIe DEVICE") |
81 | | - return pciList |
82 | | - |
83 | | - # check the current PCIe device with config file and return the result |
84 | | - def get_pcie_check(self): |
85 | | - self.load_config_file() |
86 | | - infoList = [] |
87 | | - infoDict = {} |
88 | | - err = 0 |
89 | | - curInfo = self.get_pcie_device() |
90 | | - for item_conf in self.confInfo: |
91 | | - flag = 0 |
92 | | - bus_conf = item_conf["bus"] |
93 | | - dev_conf = item_conf["dev"] |
94 | | - fn_conf = item_conf["fn"] |
95 | | - name_conf = item_conf["name"] |
96 | | - id_conf = item_conf["id"] |
97 | | - for item_cur in curInfo: |
98 | | - bus_cur = item_cur["bus"] |
99 | | - dev_cur = item_cur["dev"] |
100 | | - fn_cur = item_cur["fn"] |
101 | | - name_cur = item_cur["name"] |
102 | | - id_cur = item_cur["id"] |
103 | | - if bus_cur == bus_conf and dev_cur == dev_conf and \ |
104 | | - fn_cur == fn_conf and name_cur == name_conf and \ |
105 | | - id_cur == id_conf: |
106 | | - flag+=1 |
107 | | - continue |
108 | | - if flag: |
109 | | - item_conf["result"] = "Passed" |
110 | | - else: |
111 | | - item_conf["result"] = "Failed" |
112 | | - err+=1 |
113 | | - return self.confInfo |
114 | | - |
115 | | - # generate the config file with current pci device |
116 | | - def dump_conf_yaml(self): |
117 | | - curInfo = self.get_pcie_device() |
118 | | - with open(self.config_path + "/" + "pcie.yaml", "w") as conf_file: |
119 | | - yaml.dump(curInfo, conf_file, default_flow_style=False) |
120 | | - return |
| 1 | +# pcie_common.py |
| 2 | +# Common PCIE check interfaces for SONIC |
| 3 | +# |
| 4 | + |
| 5 | +import os |
| 6 | +import yaml |
| 7 | +import subprocess |
| 8 | +import re |
| 9 | +import sys |
| 10 | +from copy import deepcopy |
| 11 | +try: |
| 12 | + from .pcie_base import PcieBase |
| 13 | +except ImportError as e: |
| 14 | + raise ImportError(str(e) + "- required module not found") |
| 15 | + |
| 16 | + |
| 17 | +class PcieUtil(PcieBase): |
| 18 | + """Platform-specific PCIEutil class""" |
| 19 | + # got the config file path |
| 20 | + def __init__(self, path): |
| 21 | + self.config_path = path |
| 22 | + |
| 23 | + # load the config file |
| 24 | + def load_config_file(self): |
| 25 | + config_file = self.config_path + "/" + "pcie.yaml" |
| 26 | + try: |
| 27 | + with open(config_file) as conf_file: |
| 28 | + self.confInfo = yaml.load(conf_file) |
| 29 | + except IOError as e: |
| 30 | + print "Error: {}".format(str(e)) |
| 31 | + print "Not found config file, please add a config file manually, or generate it by running [pcieutil pcie_generate]" |
| 32 | + sys.exit() |
| 33 | + |
| 34 | + # load current PCIe device |
| 35 | + def get_pcie_device(self): |
| 36 | + pciDict = {} |
| 37 | + pciList = [] |
| 38 | + p1 = "^(\w+):(\w+)\.(\w)\s(.*)\s*\(*.*\)*" |
| 39 | + p2 = "^.*:.*:.*:(\w+)\s*\(*.*\)*" |
| 40 | + command1 = "sudo lspci" |
| 41 | + command2 = "sudo lspci -n" |
| 42 | + # run command 1 |
| 43 | + proc1 = subprocess.Popen(command1, shell=True, stdout=subprocess.PIPE) |
| 44 | + output1 = proc1.stdout.readlines() |
| 45 | + (out, err) = proc1.communicate() |
| 46 | + # run command 2 |
| 47 | + proc2 = subprocess.Popen(command2, shell=True, stdout=subprocess.PIPE) |
| 48 | + output2 = proc2.stdout.readlines() |
| 49 | + (out, err) = proc2.communicate() |
| 50 | + |
| 51 | + if proc1.returncode > 0: |
| 52 | + for line1 in output1: |
| 53 | + print(line1.strip()) |
| 54 | + return |
| 55 | + elif proc2.returncode > 0: |
| 56 | + for line2 in output2: |
| 57 | + print(line2.strip()) |
| 58 | + return |
| 59 | + else: |
| 60 | + for (line1, line2) in zip(output1, output2): |
| 61 | + pciDict.clear() |
| 62 | + match1 = re.search(p1, line1.strip()) |
| 63 | + match2 = re.search(p2, line2.strip()) |
| 64 | + if match1 and match2: |
| 65 | + Bus = match1.group(1) |
| 66 | + Dev = match1.group(2) |
| 67 | + Fn = match1.group(3) |
| 68 | + Name = match1.group(4) |
| 69 | + Id = match2.group(1) |
| 70 | + pciDict["name"] = Name |
| 71 | + pciDict["bus"] = Bus |
| 72 | + pciDict["dev"] = Dev |
| 73 | + pciDict["fn"] = Fn |
| 74 | + pciDict["id"] = Id |
| 75 | + pciList.append(pciDict) |
| 76 | + pciDict = deepcopy(pciDict) |
| 77 | + else: |
| 78 | + print("CAN NOT MATCH PCIe DEVICE") |
| 79 | + return pciList |
| 80 | + |
| 81 | + # check the sysfs tree for each PCIe device |
| 82 | + def check_pcie_sysfs(self, domain=0, bus=0, device=0, func=0): |
| 83 | + dev_path = os.path.join('/sys/bus/pci/devices', '%04x:%02x:%02x.%d' % (domain, bus, device, func)) |
| 84 | + if os.path.exists(dev_path): |
| 85 | + return True |
| 86 | + return False |
| 87 | + |
| 88 | + # check the current PCIe device with config file and return the result |
| 89 | + def get_pcie_check(self): |
| 90 | + self.load_config_file() |
| 91 | + for item_conf in self.confInfo: |
| 92 | + bus_conf = item_conf["bus"] |
| 93 | + dev_conf = item_conf["dev"] |
| 94 | + fn_conf = item_conf["fn"] |
| 95 | + if self.check_pcie_sysfs(bus=int(bus_conf, base=16), device=int(dev_conf, base=16), func=int(fn_conf, base=16)): |
| 96 | + item_conf["result"] = "Passed" |
| 97 | + else: |
| 98 | + item_conf["result"] = "Failed" |
| 99 | + return self.confInfo |
| 100 | + |
| 101 | + # generate the config file with current pci device |
| 102 | + def dump_conf_yaml(self): |
| 103 | + curInfo = self.get_pcie_device() |
| 104 | + with open(self.config_path + "/" + "pcie.yaml", "w") as conf_file: |
| 105 | + yaml.dump(curInfo, conf_file, default_flow_style=False) |
| 106 | + return |
0 commit comments