From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 293AD1FF065 for ; Mon, 23 Nov 2026 14:37:04 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 73D9A2174A; Mon, 23 Nov 2026 14:36:00 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v5 storage 09/26] api: scan: add NVMe over Fabrics discovery Date: Mon, 23 Nov 2026 14:32:01 +0100 Message-ID: <20261123133412.3710030-10-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123133412.3710030-1-t.lamprecht@proxmox.com> References: <20261123133412.3710030-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795440864373 X-SPAM-LEVEL: Spam detection results: 0 AWL -1.003 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) KAM_MAILER 2 Automated Mailer Tag Left in Email POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: QFCXTQDFWKLFUYEH45HOYMFLZB6NQBS2 X-Message-ID-Hash: QFCXTQDFWKLFUYEH45HOYMFLZB6NQBS2 X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Setting up an NVMe over Fabrics storage needs the NQN of the subsystem and its portals. Add /nodes/{node}/scan/nvmeof, which asks the discovery service at a portal for the NVMe/TCP subsystems it offers to the node and lists them with their portals. Without a port, it tries the discovery port 8009 and then the I/O port 4420, where Linux targets answer discovery too. The scan needs Datastore.Allocate on /storage, like the iSCSI scan. Signed-off-by: Thomas Lamprecht --- src/PVE/API2/Storage/Scan.pm | 49 ++++++++++++++++ src/PVE/NVMeOF.pm | 66 ++++++++++++++++++++++ src/test/run_nvmeof_taint_tests.pl | 5 ++ src/test/run_nvmeof_tests.pl | 90 ++++++++++++++++++++++++++++++ src/test/run_san_api_tests.pl | 2 +- 5 files changed, 211 insertions(+), 1 deletion(-) diff --git a/src/PVE/API2/Storage/Scan.pm b/src/PVE/API2/Storage/Scan.pm index f37e7d4e..c6263994 100644 --- a/src/PVE/API2/Storage/Scan.pm +++ b/src/PVE/API2/Storage/Scan.pm @@ -6,6 +6,7 @@ use warnings; # NOTE: This API endpoints are mounted by pve-manager's API2::Node module and pvesm CLI use PVE::JSONSchema qw(get_standard_option); +use PVE::NVMeOF; use PVE::RESTHandler; use PVE::SafeSyslog; @@ -49,6 +50,7 @@ __PACKAGE__->register_method({ { method => 'lvm' }, { method => 'lvmthin' }, { method => 'nfs' }, + { method => 'nvmeof' }, { method => 'pbs' }, { method => 'zfs' }, ]; @@ -300,6 +302,53 @@ __PACKAGE__->register_method({ }, }); +__PACKAGE__->register_method({ + name => 'nvmeofscan', + path => 'nvmeof', + method => 'GET', + description => + 'List the NVMe over TCP subsystems offered to this node by a discovery controller.', + protected => 1, + proxyto => 'node', + permissions => { check => ['perm', '/storage', ['Datastore.Allocate']] }, + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + portal => { + description => 'IPv4 or bracketed IPv6 discovery address with an optional port.' + . ' Without a port, try 8009 then 4420.', + type => 'string', + format => 'pve-storage-nvme-portal', + }, + }, + }, + returns => { + type => 'array', + items => { + type => 'object', + additionalProperties => 0, + properties => { + subsysnqn => { type => 'string', description => 'The subsystem NQN.' }, + portals => { + type => 'string', + description => 'The discovered TCP portals, comma separated.', + }, + }, + }, + }, + code => sub { + my ($param) = @_; + my $res = PVE::NVMeOF::discover($param->{portal}); + return [ + map { { + subsysnqn => $_, + portals => join(',', map { PVE::NVMeOF::format_path($_) } $res->{$_}->@*), + } } sort keys %$res + ]; + }, +}); + __PACKAGE__->register_method({ name => 'lvmscan', path => 'lvm', diff --git a/src/PVE/NVMeOF.pm b/src/PVE/NVMeOF.pm index 2be6d8e7..9744e471 100644 --- a/src/PVE/NVMeOF.pm +++ b/src/PVE/NVMeOF.pm @@ -7,11 +7,13 @@ use Digest::SHA qw(sha1_hex); use Fcntl qw(:flock F_GETFD F_SETFD FD_CLOEXEC); use File::Basename qw(basename); use File::Path qw(make_path); +use JSON qw(decode_json); use POSIX qw(ceil); use Socket qw(AF_INET AF_INET6 inet_ntop inet_pton); use Time::HiRes (); use PVE::JSONSchema; +use PVE::Network; use PVE::Tools qw(dir_glob_foreach file_read_firstline file_set_contents run_command); use PVE::Diskmanage; @@ -313,4 +315,68 @@ sub connect_path($scfg, $path, $limit = undef) { return $res; } +sub parse_discovery_log($raw) { + my $log = decode_json($raw); + die "invalid NVMe discovery log\n" + if ref($log) ne 'HASH' || ref($log->{records}) ne 'ARRAY'; + my (%seen, %res); + for my $record ($log->{records}->@*) { + next + if ($record->{subtype} // '') ne 'nvme subsystem' || ($record->{trtype} // '') ne 'tcp'; + # the log pads the address fields with spaces, which not every nvme-cli removes + my ($nqn_raw, $addr, $port) = + map { my $v = $_ // ''; $v =~ s/^\s+|\s+$//g; $v } + $record->@{qw(subnqn traddr trsvcid)}; + my $nqn = subsystem_nqn($nqn_raw) // next; + my $path = parse_portal(($addr =~ /:/ ? "[$addr]" : $addr) . ":$port") // next; + next if $seen{$nqn}->{ format_path($path) }++; + push $res{$nqn}->@*, $path; + } + return \%res; +} + +sub discover($portal) { + my $path = parse_portal($portal) // die "invalid portal '$portal'\n"; + my @ports = $portal =~ /:[0-9]+$/ ? ($path->{trsvcid}) : (8009, 4420); + # a missing prerequisite fails on every port, so report it as it is + assert_support(); + my $host = host_identity(); + my $error; + for my $port (@ports) { + # keep the error of a port that answered rather than "no answer" from the other port + if (!PVE::Network::tcp_ping($path->{traddr}, $port, 2)) { + $error //= "no answer from portal\n"; + next; + } + my $result = eval { + with_lock(sub { + my $out = ''; + run_command( + [ + $NVME, + 'discover', + '--transport=tcp', + '--traddr', + $path->{traddr}, + '--trsvcid', + $port, + '--hostnqn', + $host->{nqn}, + '--hostid', + $host->{id}, + '--output-format=json', + ], + timeout => 30, + errmsg => 'nvme discover', + outfunc => sub($line) { $out .= "$line\n" }, + ); + return parse_discovery_log($out); + }); + }; + return $result if !$@; + $error = $@; + } + die "discovery at portal '$portal' failed - $error"; +} + 1; diff --git a/src/test/run_nvmeof_taint_tests.pl b/src/test/run_nvmeof_taint_tests.pl index 6fd37410..ef332633 100755 --- a/src/test/run_nvmeof_taint_tests.pl +++ b/src/test/run_nvmeof_taint_tests.pl @@ -40,6 +40,7 @@ $nvme->mock( 'all connect operands are validated and untainted', ); PVE::Tools::run_command(['/usr/bin/true', @$cmd], timeout => 5); + $opts{outfunc}->('{"records":[]}') if $opts{outfunc}; }, ); my $path = PVE::NVMeOF::parse_portal($portal); @@ -55,6 +56,10 @@ for my $bad ('nqn.bad,nqn=other', "nqn.test\n", 'nqn.2014-08.org.nvmexpress.disc eval { PVE::NVMeOF::connect_path({ subsysnqn => $bad }, $path) }; like($@, qr/invalid subsystem/, 'invalid connect subsystem refused'); } +my $net = Test::MockModule->new('PVE::Network'); +$net->mock(tcp_ping => sub { 1 }); +eval { PVE::NVMeOF::discover($portal) }; +is($@, '', 'discovery can execute with file-backed host identity in taint mode'); file_set_contents($PVE::NVMeOF::HOSTID_FILE, "00000000-0000-0000-0000-000000000000\n"); eval { PVE::NVMeOF::host_identity() }; like($@, qr/nonzero UUID/, 'zero host ID refused'); diff --git a/src/test/run_nvmeof_tests.pl b/src/test/run_nvmeof_tests.pl index b9bed005..fe9bdace 100755 --- a/src/test/run_nvmeof_tests.pl +++ b/src/test/run_nvmeof_tests.pl @@ -5,6 +5,7 @@ use lib qw(..); use Fcntl qw(:flock); use File::Path qw(make_path remove_tree); use File::Temp qw(tempdir); +use JSON qw(encode_json); use POSIX (); use Test::MockModule; use Test::More; @@ -13,6 +14,7 @@ use Time::HiRes (); use PVE::Tools qw(file_set_contents); use PVE::API2::Storage::Config; +use PVE::API2::Storage::Scan; use PVE::NVMeOF; use PVE::Storage; @@ -601,4 +603,92 @@ subtest 'the connection check has a time limit too' => sub { is($pings, 0, 'which the check does not ping'); }; +subtest 'TCP discovery, fallback and API result' => sub { + my $record = { + subtype => 'nvme subsystem', + trtype => 'tcp', + subnqn => 'nqn.test', + traddr => '192.0.2.1', + trsvcid => '4420', + }; + my $raw = encode_json({ + records => + [$record, $record, { %$record, trtype => 'rdma' }, { %$record, trsvcid => 70000 }], + }); + is_deeply( + PVE::NVMeOF::parse_discovery_log($raw), + { 'nqn.test' => [{ traddr => '192.0.2.1', trsvcid => 4420 }] }, + 'TCP only, unique valid portals', + ); + my $padded = + { %$record, traddr => '192.0.2.1' . (' ' x 10), trsvcid => '4420' . (' ' x 28) }; + is_deeply( + PVE::NVMeOF::parse_discovery_log(encode_json({ records => [$padded] })), + { 'nqn.test' => [{ traddr => '192.0.2.1', trsvcid => 4420 }] }, + 'padded address fields', + ); + for my $bad ('garbage', '{}', '[]') { + eval { PVE::NVMeOF::parse_discovery_log($bad) }; + ok($@, 'malformed discovery is not an empty success'); + } + my $nvme = Test::MockModule->new('PVE::NVMeOF'); + my $net = Test::MockModule->new('PVE::Network'); + my (@ports, @commands); + our $locked; + $nvme->mock(with_lock => sub { local $locked = 1; $_[0]->() }); + $nvme->mock(assert_support => sub { }); + $nvme->mock(host_identity => sub { { nqn => 'nqn.host', id => 'id' } }); + $net->mock(tcp_ping => sub { push @ports, $_[1]; $_[1] != 8009 }); + $nvme->mock( + run_command => sub($cmd, %opts) { + ok($locked, 'discovery holds the NVMe lock'); + is($opts{timeout}, 30, 'discovery command timeout'); + push @commands, $cmd; + $opts{outfunc}->($raw); + }, + ); + my $rows = PVE::API2::Storage::Scan->nvmeofscan({ node => 'n1', portal => '192.0.2.1' }); + is_deeply(\@ports, [8009, 4420], 'unspecified port tries discovery then I/O port'); + is_deeply($rows, [{ subsysnqn => 'nqn.test', portals => '192.0.2.1:4420' }], 'API shape'); + eval { + PVE::JSONSchema::validate( + $rows, + PVE::API2::Storage::Scan->map_method_by_name('nvmeofscan')->{returns}, + ); + }; + is($@, '', 'API schema accepts result'); + @ports = (); + PVE::NVMeOF::discover('[2001:db8::1]:4421'); + is_deeply(\@ports, [4421], 'explicit port does not fall back'); + # the discovery port answers but refuses, the I/O port does not answer + $net->mock(tcp_ping => sub { $_[1] == 8009 }); + $nvme->mock(run_command => sub { die "host not allowed\n" }); + eval { PVE::NVMeOF::discover('192.0.2.1') }; + like($@, qr/failed - host not allowed$/, 'the error of the answering port is kept'); + $net->mock(tcp_ping => sub { $_[1] == 4420 }); + eval { PVE::NVMeOF::discover('192.0.2.1') }; + like($@, qr/failed - host not allowed$/, 'also if the other port answers'); + $net->mock(tcp_ping => sub { 0 }); + eval { PVE::NVMeOF::discover('192.0.2.1') }; + like($@, qr/failed - no answer from portal$/, 'without any answer'); + $nvme->mock(assert_support => sub { die "nvme-cli is not installed\n" }); + eval { PVE::NVMeOF::discover('192.0.2.1') }; + is($@, "nvme-cli is not installed\n", 'a missing prerequisite is reported as it is'); + # nvme-cli itself writes its diagnostic to stderr and exits with an error + $nvme->unmock('run_command'); + $nvme->mock(assert_support => sub { }); + my $cli = "$dir/nvme-refusing"; + file_set_contents($cli, "#!/bin/sh\necho 'host not allowed' >&2\nexit 1\n", 0755); + { + local $PVE::NVMeOF::NVME = $cli; + $net->mock(tcp_ping => sub { $_[1] == 8009 }); + eval { PVE::NVMeOF::discover('192.0.2.1') }; + like( + $@, + qr/failed - nvme discover: host not allowed$/, + 'with the diagnostic of nvme-cli', + ); + } +}; + done_testing(); diff --git a/src/test/run_san_api_tests.pl b/src/test/run_san_api_tests.pl index faa29fc3..73938a7e 100755 --- a/src/test/run_san_api_tests.pl +++ b/src/test/run_san_api_tests.pl @@ -104,7 +104,7 @@ is_deeply( ); is_deeply( [map { $_->{method} } PVE::API2::Storage::Scan->index({ node => 'test' })->@*], - [qw(cifs iscsi lvm lvmthin nfs pbs zfs)], + [qw(cifs iscsi lvm lvmthin nfs nvmeof pbs zfs)], 'scan index lists the discovery scans', ); is_deeply(