Skip to content

Commit d247304

Browse files
committed
Update pcie_common.py
Adding domain-aware PCIe handling and improve pcie_common robustness Signed-off-by: t2sharma <dhananjais009@gmail.com>
1 parent a3df22e commit d247304

1 file changed

Lines changed: 73 additions & 44 deletions

File tree

sonic_platform_base/sonic_pcie/pcie_common.py

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,49 +35,70 @@ def load_config_file(self):
3535

3636
# load current PCIe device
3737
def get_pcie_device(self):
38-
pciDict = {}
3938
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, universal_newlines=True, stdout=subprocess.PIPE)
46-
output1 = proc1.stdout.readlines()
47-
(out, err) = proc1.communicate()
48-
# run command 2
49-
proc2 = subprocess.Popen(command2, universal_newlines=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")
39+
seen = set()
40+
41+
# Domain-aware output:
42+
# 0002:01:00.0 Ethernet controller: ...
43+
p1 = r"^([0-9a-fA-F]{4}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2})\.([0-7])\s+(.*)$"
44+
# Numeric output:
45+
# 0002:01:00.0 0200: 177d:a065 ...
46+
p2 = r"^[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-7]\s+[0-9a-fA-F]{4}:\s*([0-9a-fA-F]{4}):([0-9a-fA-F]{4}).*$"
47+
48+
command1 = ["sudo", "lspci", "-D"]
49+
command2 = ["sudo", "lspci", "-D", "-n"]
50+
51+
proc1 = subprocess.Popen(command1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
52+
output1, err1 = proc1.communicate()
53+
54+
proc2 = subprocess.Popen(command2, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55+
output2, err2 = proc2.communicate()
56+
57+
if proc1.returncode != 0:
58+
print(output1)
59+
print(err1)
60+
return []
61+
if proc2.returncode != 0:
62+
print(output2)
63+
print(err2)
64+
return []
65+
66+
lines1 = output1.splitlines()
67+
lines2 = output2.splitlines()
68+
69+
for line1, line2 in zip(lines1, lines2):
70+
match1 = re.search(p1, line1.strip())
71+
match2 = re.search(p2, line2.strip())
72+
if not (match1 and match2):
73+
print("CAN NOT MATCH PCIe DEVICE")
74+
print("lspci :", line1.strip())
75+
print("lspci -n:", line2.strip())
76+
continue
77+
78+
domain = match1.group(1).lower()
79+
bus = match1.group(2).lower()
80+
dev = match1.group(3).lower()
81+
fn = match1.group(4).lower()
82+
name = match1.group(5).strip()
83+
vendor = match2.group(1).lower()
84+
device = match2.group(2).lower()
85+
86+
key = (domain, bus, dev, fn)
87+
if key in seen:
88+
continue
89+
seen.add(key)
90+
91+
pciDict = {
92+
"name": name,
93+
"domain": domain,
94+
"bus": bus,
95+
"dev": dev,
96+
"fn": fn,
97+
"id": device,
98+
"vendor": vendor
99+
}
100+
pciList.append(deepcopy(pciDict))
101+
81102
return pciList
82103

83104
# check the sysfs tree for each PCIe device
@@ -91,10 +112,17 @@ def check_pcie_sysfs(self, domain=0, bus=0, device=0, func=0):
91112
def get_pcie_check(self):
92113
self.load_config_file()
93114
for item_conf in self.confInfo:
115+
domain_conf = item_conf.get("domain", "0000")
94116
bus_conf = item_conf["bus"]
95117
dev_conf = item_conf["dev"]
96118
fn_conf = item_conf["fn"]
97-
if self.check_pcie_sysfs(bus=int(bus_conf, base=16), device=int(dev_conf, base=16), func=int(fn_conf, base=16)):
119+
120+
if self.check_pcie_sysfs(
121+
domain=int(domain_conf, base=16),
122+
bus=int(bus_conf, base=16),
123+
device=int(dev_conf, base=16),
124+
func=int(fn_conf, base=16)
125+
):
98126
item_conf["result"] = "Passed"
99127
else:
100128
item_conf["result"] = "Failed"
@@ -142,5 +170,6 @@ def dump_conf_yaml(self):
142170
conf_rev = "_{}".format(self._conf_rev) if self._conf_rev else ""
143171
config_file = "{}/pcie{}.yaml".format(self.config_path, conf_rev)
144172
with open(config_file, "w") as conf_file:
145-
yaml.dump(curInfo, conf_file, default_flow_style=False)
173+
yaml.dump(curInfo, conf_file, default_flow_style=False, sort_keys=False)
146174
return
175+

0 commit comments

Comments
 (0)