Skip to content

Commit 3d7137d

Browse files
author
Martin Belanger
committed
examples: LID supported must be shifted right by 16
To access the LID Specific Field (see Base specs) the values from the Get Supported Log pages must be shifted right by 16. The example code was missing the shift. Also added better exception handling example. Signed-off-by: Martin Belanger <[email protected]>
1 parent 2e05482 commit 3d7137d

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

examples/discover-loop.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import pprint
2222
from libnvme import nvme
2323

24-
def disc_supp_str(disc_log_page_support):
24+
def disc_supp_str(dlp_supp_opts):
2525
d = {
2626
nvme.NVMF_LOG_DISC_LID_EXTDLPES: "Extended Discovery Log Page Entry Supported (EXTDLPES)",
2727
nvme.NVMF_LOG_DISC_LID_PLEOS: "Port Local Entries Only Supported (PLEOS)",
2828
nvme.NVMF_LOG_DISC_LID_ALLSUBES: "All NVM Subsystem Entries Supported (ALLSUBES)",
2929
}
30-
return [txt for msk, txt in d.items() if disc_log_page_support & msk]
30+
return [txt for msk, txt in d.items() if dlp_supp_opts & msk]
3131

3232
r = nvme.root()
3333
h = nvme.host(r)
@@ -40,11 +40,16 @@ def disc_supp_str(disc_log_page_support):
4040
print("connected to %s subsys %s" % (c.name, c.subsystem.name))
4141

4242
slp = c.supported_log_pages()
43-
disc_log_page_support = slp[nvme.NVME_LOG_LID_DISCOVER] if slp is not None else 0
44-
print(f"LID {nvme.NVME_LOG_LID_DISCOVER}h (Discovery), supports: {disc_supp_str(disc_log_page_support)}")
4543

4644
try:
47-
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if disc_log_page_support & nvme.NVMF_LOG_DISC_LID_PLEOS else 0
45+
dlp_supp_opts = slp[nvme.NVME_LOG_LID_DISCOVER] >> 16
46+
except (TypeError, IndexError):
47+
dlp_supp_opts = 0
48+
49+
print(f"LID {nvme.NVME_LOG_LID_DISCOVER}h (Discovery), supports: {disc_supp_str(dlp_supp_opts)}")
50+
51+
try:
52+
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0
4853
d = c.discover(lsp=lsp)
4954
print(pprint.pformat(d))
5055
except Exception as e:

libnvme/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import sys
1010
import pprint
1111
from libnvme import nvme
1212

13-
def disc_supp_str(disc_log_page_support):
14-
d = {
15-
nvme.NVMF_LOG_DISC_LID_EXTDLPES: "Extended Discovery Log Page Entry Supported (EXTDLPES)",
16-
nvme.NVMF_LOG_DISC_LID_PLEOS: "Port Local Entries Only Supported (PLEOS)",
17-
nvme.NVMF_LOG_DISC_LID_ALLSUBES: "All NVM Subsystem Entries Supported (ALLSUBES)",
13+
def disc_supp_str(dlp_supp_opts):
14+
bitmap = {
15+
nvme.NVMF_LOG_DISC_LID_EXTDLPES: "EXTDLPES",
16+
nvme.NVMF_LOG_DISC_LID_PLEOS: "PLEOS",
17+
nvme.NVMF_LOG_DISC_LID_ALLSUBES: "ALLSUBES",
1818
}
19-
return [txt for msk, txt in d.items() if disc_log_page_support & msk]
19+
return [txt for msk, txt in bitmap.items() if dlp_supp_opts & msk]
2020

2121
root = nvme.root() # This is a singleton
2222
root.log_level('debug') # Optional: extra debug info
@@ -40,15 +40,15 @@ except Exception as e:
4040
sys.exit(f'Failed to connect: {e}')
4141

4242
supported_log_pages = ctrl.supported_log_pages()
43-
if supported_log_pages is not None:
44-
disc_log_page_support = supported_log_pages[nvme.NVME_LOG_LID_DISCOVER]
45-
print(f"LID {nvme.NVME_LOG_LID_DISCOVER:02x}h (Discovery), supports: {disc_supp_str(disc_log_page_support)}")
43+
try:
44+
# Get the supported options for the Get Discovery Log Page command
45+
dlp_supp_opts = supported_log_pages[nvme.NVME_LOG_LID_DISCOVER] >> 16
46+
except (TypeError, IndexError):
47+
dlp_supp_opts = 0
4648

49+
print(f"LID {nvme.NVME_LOG_LID_DISCOVER:02x}h (Discovery), supports: {disc_supp_str(dlp_supp_opts)}")
4750
try:
48-
if disc_log_page_support and (disc_log_page_support & nvme.NVMF_LOG_DISC_LID_PLEOS):
49-
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO
50-
else:
51-
lsp = 0
51+
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0
5252
log_pages = ctrl.discover(lsp=lsp)
5353
print(pprint.pformat(log_pages))
5454
except Exception as e:

0 commit comments

Comments
 (0)