-
Notifications
You must be signed in to change notification settings - Fork 8
/
common_bf.py
87 lines (70 loc) · 1.99 KB
/
common_bf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import dataclasses
import os
import shlex
import subprocess
import sys
from typing import Optional
@dataclasses.dataclass(frozen=True)
class Result:
out: str
err: str
returncode: int
def run(cmd: str, env: dict[str, str] = os.environ.copy()) -> Result:
args = shlex.split(cmd)
res = subprocess.run(
args,
capture_output=True,
env=env,
)
return Result(
out=res.stdout.decode("utf-8"),
err=res.stderr.decode("utf-8"),
returncode=res.returncode,
)
def all_interfaces() -> dict[str, str]:
out = run("lshw -c network -businfo").out
ret = {}
for e in out.split("\n")[2:]:
e = e.strip()
if not e:
continue
pci, dev = e.split()[0:2]
before_network = e.split("network")[0].strip()
desc = e[len(before_network) :].strip()[len("network") :].strip()
ret[pci] = desc
return ret
def find_bf_pci_addresses() -> list[str]:
ai = all_interfaces()
bfs = [e for e in ai.items() if "BlueField" in e[1]]
return [k.split("@")[1] for k, v in bfs]
def find_bf_pci_addresses_or_quit(bf_id: int) -> str:
bf_pci = find_bf_pci_addresses()
if not bf_pci:
print("No BF found")
sys.exit(-1)
if bf_id < 0 or bf_id >= len(bf_pci):
print("Invalid ID for BF")
sys.exit(-1)
return bf_pci[bf_id]
def mst_flint(pci: str) -> dict[str, str]:
out = run(f"mstflint -d {pci} q").out
ret = {}
for e in out.split("\n"):
e = e.strip()
if not e:
continue
esplit = e.split(":")
if len(esplit) != 2:
continue
key, value = esplit
key = key.strip()
value = value.strip()
ret[key] = value
return ret
def bf_version(pci: str) -> Optional[int]:
out = run("lshw -c network -businfo").out
for e in out.split("\n"):
if not e.startswith(f"pci@{pci}"):
continue
return int(e.split("BlueField-")[1].split()[0])
return None