Skip to content

Commit 284659b

Browse files
hreineckeigaw
authored andcommitted
examples/discovery-loop.py: update to use tcp transport
Update the discovery-loop.py example to use the tcp transport and fix an issue where the controller wasn't disconnected when the disovery failed. Signed-off-by: Hannes Reinecke <hare@suse.de>
1 parent 0ee7f02 commit 284659b

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

examples/discover-loop.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22
# SPDX-License-Identifier: Apache-2.0
33
'''
44
Example script for nvme discovery
5-
6-
Copyright (c) 2021 Hannes Reinecke, SUSE Software Solutions
7-
Licensed under the Apache License, Version 2.0 (the "License"); you may
8-
not use this file except in compliance with the License. You may obtain
9-
a copy of the License at
10-
11-
http://www.apache.org/licenses/LICENSE-2.0
12-
13-
Unless required by applicable law or agreed to in writing, software
14-
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15-
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16-
License for the specific language governing permissions and limitations
17-
under the License.
185
'''
196

207
import sys
@@ -29,17 +16,24 @@ def disc_supp_str(dlp_supp_opts):
2916
}
3017
return [txt for msk, txt in d.items() if dlp_supp_opts & msk]
3118

32-
r = nvme.root()
33-
h = nvme.host(r)
34-
c = nvme.ctrl(r, nvme.NVME_DISC_SUBSYS_NAME, 'loop')
19+
root = nvme.root()
20+
host = nvme.host(root)
21+
22+
subsysnqn = nvme.NVME_DISC_SUBSYS_NAME
23+
transport = 'tcp'
24+
traddr = '127.0.0.1'
25+
trsvcid = '4420'
26+
27+
ctrl = nvme.ctrl(root, subsysnqn=subsysnqn, transport=transport, traddr=traddr, trsvcid=trsvcid)
28+
3529
try:
36-
c.connect(h)
30+
ctrl.connect(host)
3731
except Exception as e:
3832
sys.exit(f'Failed to connect: {e}')
3933

40-
print("connected to %s subsys %s" % (c.name, c.subsystem.name))
34+
print(f'{ctrl.name} connected to subsys {ctrl.subsystem}')
4135

42-
slp = c.supported_log_pages()
36+
slp = ctrl.supported_log_pages()
4337

4438
try:
4539
dlp_supp_opts = slp[nvme.NVME_LOG_LID_DISCOVER] >> 16
@@ -50,16 +44,20 @@ def disc_supp_str(dlp_supp_opts):
5044

5145
try:
5246
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0
53-
d = c.discover(lsp=lsp)
54-
print(pprint.pformat(d))
47+
disc_log = ctrl.discover(lsp=lsp)
5548
except Exception as e:
5649
sys.exit(f'Failed to discover: {e}')
50+
disc_log = []
51+
pass
52+
53+
for dlpe in disc_log:
54+
print(f'log entry {dlpe["portid"]}: {dlpe["subtype"]} {dlpe["subnqn"]}')
5755

5856
try:
59-
c.disconnect()
57+
ctrl.disconnect()
6058
except Exception as e:
6159
sys.exit(f'Failed to disconnect: {e}')
6260

63-
c = None
64-
h = None
65-
r = None
61+
for s in host.subsystems():
62+
for c in s.controllers():
63+
print(f'{s}: {c.name}')

0 commit comments

Comments
 (0)