| 1 |
#!/usr/bin/python3 |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
''' |
| 4 |
Example script for nvme discovery |
| 5 |
''' |
| 6 |
|
| 7 |
import sys |
| 8 |
import pprint |
| 9 |
from libnvme import nvme |
| 10 |
|
| 11 |
def disc_supp_str(dlp_supp_opts): |
| 12 |
d = { |
| 13 |
nvme.NVMF_LOG_DISC_LID_EXTDLPES: "Extended Discovery Log Page Entry Supported (EXTDLPES)", |
| 14 |
nvme.NVMF_LOG_DISC_LID_PLEOS: "Port Local Entries Only Supported (PLEOS)", |
| 15 |
nvme.NVMF_LOG_DISC_LID_ALLSUBES: "All NVM Subsystem Entries Supported (ALLSUBES)", |
| 16 |
} |
| 17 |
return [txt for msk, txt in d.items() if dlp_supp_opts & msk] |
| 18 |
|
| 19 |
def discover(host, ctrl, iteration): |
| 20 |
# Only 8 levels of indirection are supported |
| 21 |
if iteration > 8: |
| 22 |
return |
| 23 |
|
| 24 |
try: |
| 25 |
ctrl.connect(host) |
| 26 |
except Exception as e: |
| 27 |
print(f'Failed to connect: {e}') |
| 28 |
return |
| 29 |
|
| 30 |
print(f'{ctrl.name} connected to {ctrl.subsystem}') |
| 31 |
|
| 32 |
slp = ctrl.supported_log_pages() |
| 33 |
try: |
| 34 |
dlp_supp_opts = slp[nvme.NVME_LOG_LID_DISCOVER] >> 16 |
| 35 |
except (TypeError, IndexError): |
| 36 |
dlp_supp_opts = 0 |
| 37 |
|
| 38 |
print(f"LID {nvme.NVME_LOG_LID_DISCOVER}h (Discovery), supports: {disc_supp_str(dlp_supp_opts)}") |
| 39 |
|
| 40 |
try: |
| 41 |
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0 |
| 42 |
disc_log = ctrl.discover(lsp=lsp) |
| 43 |
except Exception as e: |
| 44 |
print(f'Failed to discover: {e}') |
| 45 |
return |
| 46 |
|
| 47 |
for dlpe in disc_log: |
| 48 |
if dlpe['subtype'] == 'nvme': |
| 49 |
print(f'{iteration}: {dlpe["subtype"]} {dlpe["subnqn"]}') |
| 50 |
continue |
| 51 |
if dlpe['subtype'] == 'discovery' and dlpe['subnqn'] == nvme.NVME_DISC_SUBSYS_NAME: |
| 52 |
continue |
| 53 |
print(f'{iteration}: {dlpe["subtype"]} {dlpe["subnqn"]}') |
| 54 |
with nvme.ctrl(root, subsysnqn=dlpe['subnqn'], transport=dlpe['trtype'], traddr=dlpe['traddr'], trsvcid=dlpe['trsvcid']) as new_ctrl: |
| 55 |
discover(host, new_ctrl, iteration + 1) |
| 56 |
|
| 57 |
root = nvme.root() |
| 58 |
host = nvme.host(root) |
| 59 |
|
| 60 |
subsysnqn = nvme.NVME_DISC_SUBSYS_NAME |
| 61 |
transport = 'tcp' |
| 62 |
traddr = '127.0.0.1' |
| 63 |
trsvcid = '4420' |
| 64 |
|
| 65 |
with nvme.ctrl(root, subsysnqn=subsysnqn, transport=transport, traddr=traddr, trsvcid=trsvcid) as ctrl: |
| 66 |
discover(host, ctrl, 0) |
| 67 |
|
| 68 |
for s in host.subsystems(): |
| 69 |
for c in s.controllers(): |
| 70 |
print(f'{s}: {c.name}') |