public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Joaquin Varela <joaquinvarela@neatech.ar>
To: pve-devel@lists.proxmox.com
Subject: [PATCH storage v5 10/13] test: zfsnvme: test the target parsers and planners
Date: Thu, 26 Nov 2026 12:09:51 -0300	[thread overview]
Message-ID: <20261126150956.310593-11-joaquinvarela@neatech.ar> (raw)
In-Reply-To: <20261126150956.310593-1-joaquinvarela@neatech.ar>

Run the parsers and planners on the fixture reads and on target states
built with FakeTarget.

Signed-off-by: Joaquin Varela <joaquinvarela@neatech.ar>
---
 src/test/zfsnvme_target_test.pm | 912 ++++++++++++++++++++++++++++++++
 1 file changed, 912 insertions(+)

diff --git a/src/test/zfsnvme_target_test.pm b/src/test/zfsnvme_target_test.pm
index 22b9585b..a47a4d03 100644
--- a/src/test/zfsnvme_target_test.pm
+++ b/src/test/zfsnvme_target_test.pm
@@ -1575,4 +1575,916 @@ subtest 'the fake target answers in the formats of the fixture target' => sub {
     );
 };
 
+sub zfs_rows($name, $type, $nqn = '-', $nsid = '-', $uuid = '-', $source = 'local', $last = '-') {
+    my $source_of = sub($value) { $value eq '-' ? '-' : $source };
+    return (
+        "$name\ttype\t$type\t-",
+        "$name\tproxmox:nvme-subsys\t$nqn\t" . $source_of->($nqn),
+        "$name\tproxmox:nvme-nsid\t$nsid\t" . $source_of->($nsid),
+        "$name\tproxmox:nvme-uuid\t$uuid\t" . $source_of->($uuid),
+        "$name\tproxmox:nvme-last-nsid\t$last\t" . $source_of->($last),
+    );
+}
+
+sub inventory(@rows) {
+    return nv('_nvmet_parse_zfs_inventory', join('', map { "$_\n" } @rows), 'tank');
+}
+
+my @POOL = zfs_rows('tank', 'filesystem');
+my @ONE = zfs_rows('tank/vm-100-disk-0', 'volume', $NQN, 1, $U{1});
+my @TWO = zfs_rows('tank/vm-101-disk-0', 'volume', $NQN, 2, $U{2});
+
+subtest 'ZFS inventory parser' => sub {
+    is_deeply(
+        inventory(@POOL, @ONE, @TWO),
+        {
+            types => {
+                tank => 'filesystem',
+                'tank/vm-100-disk-0' => 'volume',
+                'tank/vm-101-disk-0' => 'volume',
+            },
+            volumes => {
+                'tank/vm-100-disk-0' => { nqn => $NQN, nsid => 1, uuid => $U{1} },
+                'tank/vm-101-disk-0' => { nqn => $NQN, nsid => 2, uuid => $U{2} },
+            },
+            last_nsid => 0,
+        },
+        'complete inventory',
+    );
+    my $pool_last = sub($last, $source = 'local') {
+        return inventory(
+            zfs_rows('tank', 'filesystem', '-', '-', '-', $source, $last),
+            zfs_rows('tank/vm-100-disk-0', 'volume', $NQN, 1, $U{1}, 'local', '-'),
+        )->{last_nsid};
+    };
+    is($pool_last->(7), 7, 'the last NSID handed out is read from the pool');
+    is($pool_last->(7, 'inherited from tank'), 0, 'but not inherited');
+    is($pool_last->('x'), 0, 'and only when it is a valid NSID');
+    is(
+        inventory(@POOL, zfs_rows('tank/vm-100-disk-0', 'volume', $NQN, 1, $U{1}, 'local', 9))
+            ->{last_nsid},
+        0,
+        'a volume does not carry it',
+    );
+    my $short_owner = "tank/vm-101-disk-0\tproxmox:nvme-subsys\t$NQN";
+    my $foreign_source = "tank/vm-101-disk-0\tproxmox:nvme-uuid\t$U{2}\tgarbage";
+    for my $case (
+        [
+            'missing owner row',
+            [@POOL, @ONE, @TWO[0, 2, 3]],
+            qr/incomplete ZFS properties on 'tank\/vm-101-disk-0'/,
+        ],
+        [
+            'short row',
+            [@POOL, @ONE, $TWO[0], $short_owner, @TWO[2, 3]],
+            qr/malformed ZFS inventory row/,
+        ],
+        [
+            'duplicate property',
+            [@POOL, @ONE, @TWO, $TWO[1]],
+            qr/duplicate ZFS property 'proxmox:nvme-subsys'/,
+        ],
+        [
+            'unknown property',
+            [@POOL, @ONE, @TWO, "tank/vm-101-disk-0\tcompression\ton\tlocal"],
+            qr/unexpected ZFS property on 'tank\/vm-101-disk-0'/,
+        ],
+        [
+            'extra field',
+            [@POOL, @ONE, @TWO[0 .. 2], "$TWO[3]\textra"],
+            qr/malformed ZFS inventory row/,
+        ],
+        [
+            'unknown source',
+            [@POOL, @ONE, @TWO[0 .. 2], $foreign_source],
+            qr/invalid ZFS property source/,
+        ],
+        ['missing pool rows', [@ONE, @TWO], qr/missing the configured pool/],
+        [
+            'missing pool type row',
+            [@POOL[1 .. 4], @ONE],
+            qr/incomplete ZFS properties on 'tank'/,
+        ],
+        [
+            'pool name prefix',
+            [@POOL, zfs_rows('tank2/vm-1-disk-0', 'volume')],
+            qr/outside configured pool/,
+        ],
+        [
+            'depth 2 dataset',
+            [@POOL, zfs_rows('tank/sub/vm-1-disk-0', 'volume')],
+            qr/outside configured pool/,
+        ],
+        [
+            'invalid type',
+            [@POOL, "tank/vm-100-disk-0\ttype\tsnapshot\t-", @ONE[1 .. 3]],
+            qr/invalid ZFS dataset type/,
+        ],
+        [
+            'value with CR',
+            [@POOL, @ONE[0 .. 2], "tank/vm-100-disk-0\tproxmox:nvme-uuid\t$U{1}\r\tlocal"],
+            qr/malformed ZFS inventory row/,
+        ],
+        [
+            'inherited from a malformed pool',
+            [
+                @POOL,
+                @ONE[0 .. 2],
+                "tank/vm-100-disk-0\tproxmox:nvme-uuid\t$U{1}\tinherited from bad pool",
+            ],
+            qr/invalid ZFS pool name/,
+        ],
+    ) {
+        my ($name, $rows, $error) = $case->@*;
+        eval { inventory($rows->@*) };
+        like($@, $error, "rejects $name");
+    }
+
+    is_deeply(
+        inventory(@POOL),
+        { types => { tank => 'filesystem' }, volumes => {}, last_nsid => 0 },
+        'a verified empty pool',
+    );
+    my $received = inventory(
+        @POOL, zfs_rows('tank/vm-100-disk-0', 'volume', $NQN, 1, $U{1}, 'received'),
+    );
+    is($received->{volumes}->{'tank/vm-100-disk-0'}->{nqn}, $NQN, 'received properties count');
+    my $inherited = inventory(
+        zfs_rows('tank', 'filesystem', $NQN, '-', '-'),
+        zfs_rows('tank/vm-100-disk-0', 'volume', $NQN, 1, $U{1}, 'inherited from tank'),
+    );
+    is_deeply(
+        $inherited->{volumes}->{'tank/vm-100-disk-0'},
+        { nqn => '-', nsid => '-', uuid => '-' },
+        'inherited properties never own a volume',
+    );
+    my $subvol = inventory(@POOL, zfs_rows('tank/subvol-100-disk-0', 'filesystem'));
+    ok(
+        $subvol->{types}->{'tank/subvol-100-disk-0'} eq 'filesystem'
+            && !$subvol->{volumes}->{'tank/subvol-100-disk-0'},
+        'a filesystem child is a dataset but not a volume',
+    );
+
+    # a direct child with a name PVE never uses is carried as not owned
+    my $spaced = inventory(@POOL, zfs_rows('tank/my data', 'volume', $NQN, 9, $U{9}));
+    is($spaced->{types}->{'tank/my data'}, 'volume',
+        'a name with a space is kept as a dataset');
+    is($spaced->{volumes}->{'tank/my data'}->{nqn}, '-', 'a name with a space is never owned');
+    is_deeply(nv('_nvmet_desired_namespaces', $spaced, $NQN), {}, 'and never exported');
+    eval { inventory(@POOL, "tank/my data\tcompression\ton\tlocal") };
+    is($@, "unexpected ZFS property\n", 'errors do not echo names PVE never uses');
+
+    my $fixture = nv('_nvmet_parse_zfs_inventory', $FIXTURE_ZFS, 'tank');
+    is_deeply(
+        $fixture->{volumes},
+        {
+            'tank/vm-100-disk-0' => { nqn => $FIXTURE_NQN, nsid => 5, uuid => $FIXTURE_UUID5 },
+            'tank/vm-100-cloudinit' =>
+                { nqn => $FIXTURE_NQN, nsid => 6, uuid => $FIXTURE_UUID6 },
+        },
+        'the fixture inventory',
+    );
+    eval { nv('_nvmet_parse_zfs_inventory', $FIXTURE_ZFS, 'bad pool') };
+    like($@, qr/invalid ZFS pool name/, 'the configured pool is validated');
+};
+
+subtest 'ZFS property values and sources' => sub {
+    for my $case (
+        [undef, 'local'],
+        ['', 'local'],
+        ["a\nb", 'local'],
+        ["a\tb", 'local'],
+        ['x', ''],
+        ['x', undef],
+        ['x', "local\r"],
+    ) {
+        eval { nv('_nvmet_property_value', $case->@*) };
+        like($@, qr/malformed ZFS property value/, 'rejects a malformed value or source');
+    }
+    is(nv('_nvmet_property_value', $NQN, 'local'), $NQN, 'local values count');
+    is(nv('_nvmet_property_value', $NQN, 'received'), $NQN, 'received values count');
+    is(nv('_nvmet_property_value', '-', '-'), '-', 'unset values');
+    is(
+        nv('_nvmet_property_value', $NQN, 'inherited from tank/a'),
+        '-',
+        'inherited values do not',
+    );
+    eval { nv('_nvmet_property_value', $NQN, 'default') };
+    like($@, qr/invalid ZFS property source/, 'rejects other sources');
+    eval { nv('_nvmet_property_value', $NQN, '-') };
+    like($@, qr/invalid ZFS property source/, 'rejects a value without a source');
+    eval { nv('_nvmet_property_value', $NQN, 'inherited from tank;x') };
+    like($@, qr/invalid ZFS pool name/, 'rejects an inherited source outside pool names');
+};
+
+subtest 'configfs parser' => sub {
+    my $with_digests = nv('_nvmet_parse_configfs', $FIXTURE_CFS . $FIXTURE_DIGESTS);
+    is($with_digests->{hosts}->{$_}->{key_sha256}, $KEY_SHA, 'key digest of a fixture host')
+        for @FIXTURE_HOSTS;
+    my $binary = nv(
+        '_nvmet_parse_configfs',
+        $FIXTURE_CFS . join('', map { "$KEY_SHA *$ROOT/hosts/$_/dhchap_key\n" } @FIXTURE_HOSTS),
+    );
+    is($binary->{hosts}->{ $FIXTURE_HOSTS[0] }->{key_sha256}, $KEY_SHA, 'binary-mode digests');
+
+    my $without = sub($re) { return $FIXTURE_CFS =~ s/^$re\n//mr };
+    for my $case (
+        [
+            'missing root',
+            $without->(qr{D \Q$ROOT\E}),
+            qr/^NVMe target configfs is unavailable$/,
+        ],
+        [
+            'a namespace without enable',
+            $without->(qr{.*namespaces/5/enable:1}),
+            qr/incomplete NVMe namespace '5' of subsystem '\Q$FIXTURE_NQN\E'/,
+        ],
+        [
+            'a port without address',
+            $without->(qr{.*ports/2/addr_traddr.*}),
+            qr/incomplete NVMe port '2'/,
+        ],
+        [
+            'a duplicate attribute',
+            "$FIXTURE_CFS$ROOT/subsystems/$FIXTURE_NQN/attr_serial:X\n",
+            qr/duplicate NVMe target configfs attribute/,
+        ],
+        [
+            'an attribute without its directory',
+            "$FIXTURE_CFS$ROOT/subsystems/nqn.x:y/attr_model:Z\n",
+            qr/incomplete NVMe subsystem 'nqn.x:y'/,
+        ],
+        [
+            'a link to a missing port',
+            "${FIXTURE_CFS}L $ROOT/ports/9/subsystems/$FIXTURE_NQN\n",
+            qr/incomplete NVMe port '9'/,
+        ],
+        [
+            'a digest for a missing host',
+            "$FIXTURE_CFS$KEY_SHA  $ROOT/hosts/nqn.x:gone/dhchap_key\n",
+            qr/key digest for unknown NVMe host 'nqn.x:gone'/,
+        ],
+        [
+            'a malformed digest',
+            $FIXTURE_CFS . substr($KEY_SHA, 1) . "  $ROOT/hosts/$FIXTURE_HOSTS[0]/dhchap_key\n",
+            qr/unexpected NVMe target configfs line/,
+        ],
+        [
+            'a duplicate digest',
+            "$FIXTURE_CFS$FIXTURE_DIGESTS$FIXTURE_DIGESTS",
+            qr/duplicate DH-HMAC-CHAP key digest/,
+        ],
+    ) {
+        my ($name, $text, $error) = $case->@*;
+        eval { nv('_nvmet_parse_configfs', $text) };
+        like($@, $error, "rejects $name");
+    }
+    eval { nv('_nvmet_parse_configfs', "$FIXTURE_CFS$KEY and more\n") };
+    is($@, "unexpected NVMe target configfs line\n", 'a garbage line is rejected without echo');
+    eval { nv('_nvmet_parse_configfs', "${FIXTURE_CFS}D $ROOT/subsystems/bad name\n") };
+    is($@, "incomplete NVMe subsystem\n", 'malformed names are not echoed');
+
+    my $tolerated = nv(
+        '_nvmet_parse_configfs',
+        join(
+            '',
+            $FIXTURE_CFS,
+            "D $ROOT/ports/1/referrals/x\n",
+            "L $ROOT/subsystems/$FIXTURE_NQN/passthru/x\n",
+            "D $ROOT/subsystems/$FIXTURE_NQN/namespaces/5/ana\n",
+            "$ROOT/ports/1/param_inline_data_size:16384\n",
+            "$ROOT/subsystems/$FIXTURE_NQN/attr_version:1.3\n",
+        ),
+    );
+    is_deeply(
+        $tolerated,
+        nv('_nvmet_parse_configfs', $FIXTURE_CFS),
+        'other objects are ignored',
+    );
+    my $unset = $FIXTURE_CFS =~ s{(namespaces/6/device_path:).*}{$1(null)}r;
+    $unset =~ s{(namespaces/5/device_path:).*}{$1};
+    my $null = nv('_nvmet_parse_configfs', $unset);
+    is(
+        $null->{subsystems}->{$FIXTURE_NQN}->{namespaces}->{6}->{device_path},
+        '(null)',
+        'a (null) device',
+    );
+    is(
+        $null->{subsystems}->{$FIXTURE_NQN}->{namespaces}->{5}->{device_path},
+        '',
+        'an empty device',
+    );
+};
+
+subtest 'state marker and mount table' => sub {
+    my ($zfs, $cfs) = nv('_nvmet_split_state', "a\tb\n$MARKER\nD x\n");
+    is_deeply([$zfs, $cfs], ["a\tb\n", "D x\n"], 'splits at the marker');
+    for my $text ("a\n", "a\n$MARKER\nb\n$MARKER\n", "a\n $MARKER\n", "a\n${MARKER}x\n") {
+        eval { nv('_nvmet_split_state', $text) };
+        like(
+            $@,
+            qr/malformed NVMe target state/,
+            'rejects a missing, repeated or altered marker',
+        );
+    }
+    my $mounts = "sysfs /sys sysfs rw 0 0\nnone /sys/kernel/config configfs rw 0 0\n";
+    ok(nv('_nvmet_parse_mounts', $mounts), 'configfs is mounted');
+    ok(!nv('_nvmet_parse_mounts', "sysfs /sys sysfs rw 0 0\n"), 'configfs is not mounted');
+    ok(!nv('_nvmet_parse_mounts', "none /mnt configfs rw 0 0\n"), 'configfs elsewhere');
+    ok(!nv('_nvmet_parse_mounts', "x /sys/kernel/config tmpfs rw 0 0\n"),
+        'another file system');
+    ok(!nv('_nvmet_parse_mounts', ''), 'empty mount table');
+};
+
+my $S = "$ROOT/subsystems/$NQN";
+sub write_step($path, $value) { return { write => $path, value => $value } }
+
+# The step that makes the key attributes of hosts readable by root only.
+sub chmod_step(@hosts) {
+    return ['chmod', '0600', map { ("$_/dhchap_key", "$_/dhchap_ctrl_key") } @hosts];
+}
+
+sub cfs_model(%o) {
+    my $model = { subsystems => {}, ports => {}, hosts => {} };
+    if (!$o{no_subsystem}) {
+        $model->{subsystems}->{$NQN} = {
+            attr_model => $MODEL,
+            attr_serial => nv('_nvmet_serial', $NQN),
+            attr_allow_any_host => '0',
+            acl => {},
+            namespaces => {},
+            ($o{subsystem} // {})->%*,
+        };
+    }
+    return $model;
+}
+
+sub ns_model($uuid, $dev, $enable = '1') {
+    return { enable => $enable, device_path => $dev, device_uuid => $uuid, buffered_io => '0' };
+}
+
+sub build_steps($nsid, $uuid, $dev) {
+    my $ns = "$S/namespaces/$nsid";
+    return [
+        ['test', '-b', $dev],
+        ['mkdir', $ns],
+        write_step("$ns/device_path", $dev),
+        write_step("$ns/device_uuid", $uuid),
+        write_step("$ns/buffered_io", 0),
+        write_step("$ns/enable", 1),
+    ];
+}
+
+subtest 'owned identity' => sub {
+    my $inv = inventory(
+        @POOL,
+        @ONE,
+        @TWO,
+        zfs_rows('tank/vm-102-disk-0', 'filesystem'),
+        zfs_rows('tank/vm-103-disk-0', 'volume'),
+        zfs_rows('tank/vm-104-disk-0', 'volume', $FOREIGN_NQN, 1, $U{1}),
+    );
+    is_deeply(
+        [nv('_nvmet_owned_identity', $inv, $NQN, 'tank/vm-100-disk-0')],
+        [1, $U{1}],
+        'identity of an owned volume; the same identity under another NQN is allowed',
+    );
+    for my $case (
+        ['tank/vm-999-disk-0', qr/does not exist/],
+        ['tank/vm-102-disk-0', qr/is not a ZFS volume/],
+        ['tank/vm-103-disk-0', qr/is not owned by NVMe subsystem/],
+        ['tank/vm-104-disk-0', qr/is not owned by NVMe subsystem/],
+    ) {
+        eval { nv('_nvmet_owned_identity', $inv, $NQN, $case->[0]) };
+        like($@, $case->[1], "refuses $case->[0]");
+    }
+    for my $nsid (0, 0xffffffff, 'x', '01') {
+        my $bad =
+            inventory(@POOL, zfs_rows('tank/vm-100-disk-0', 'volume', $NQN, $nsid, $U{1}));
+        eval { nv('_nvmet_owned_identity', $bad, $NQN, 'tank/vm-100-disk-0') };
+        like($@, qr/invalid NSID/, "refuses NSID '$nsid'");
+    }
+    my $bad = inventory(@POOL, zfs_rows('tank/vm-100-disk-0', 'volume', $NQN, 1, 'nope'));
+    eval { nv('_nvmet_owned_identity', $bad, $NQN, 'tank/vm-100-disk-0') };
+    like($@, qr/invalid namespace UUID/, 'refuses a malformed UUID');
+    for my $other ([1, $U{2}], [2, $U{1}]) {
+        my $dup = inventory(
+            @POOL, @ONE, zfs_rows('tank/vm-101-disk-0', 'volume', $NQN, $other->@*),
+        );
+        eval { nv('_nvmet_owned_identity', $dup, $NQN, 'tank/vm-100-disk-0') };
+        like($@, qr/duplicate NVMe identity on 'tank\/vm-100-disk-0'/, 'refuses a duplicate');
+    }
+    is(nv('_nvmet_serial', $NQN), 'PVEZFS' . substr(sha256_hex($NQN), 0, 14), 'serial formula');
+    is(
+        nv('_nvmet_parse_configfs', $FIXTURE_CFS)->{subsystems}->{$FIXTURE_NQN}->{attr_serial},
+        nv('_nvmet_serial', $FIXTURE_NQN),
+        'the fixture subsystem carries the serial of its NQN',
+    );
+};
+
+subtest 'namespace ID allocation' => sub {
+    my $rows = sub(@ids) {
+        my $uuid = sub($id) { sprintf('%08d-0000-4000-8000-000000000000', $id) };
+        return inventory(
+            @POOL,
+            map { zfs_rows("tank/vm-$_-disk-0", 'volume', $NQN, $_, $uuid->($_)) } @ids,
+        );
+    };
+    my $cfs = cfs_model();
+    is(nv('_nvmet_allocate_nsid', inventory(@POOL), $cfs, $NQN), 1, 'first NSID');
+    is(nv('_nvmet_allocate_nsid', $rows->(1, 3), $cfs, $NQN), 4, 'a gap is not reused');
+    my $dirs =
+        cfs_model(subsystem => { namespaces => { 7 => ns_model($U{7}, '(null)', '0') } });
+    is(nv('_nvmet_allocate_nsid', $rows->(1), $dirs, $NQN), 8,
+        'configfs-only namespaces count');
+    my $foreign =
+        inventory(@POOL, zfs_rows('tank/vm-9-disk-0', 'volume', $FOREIGN_NQN, 9, $U{9}));
+    my $foreign_cfs = cfs_model();
+    $foreign_cfs->{subsystems}->{$FOREIGN_NQN} = { namespaces => { 12 => {} } };
+    is(nv('_nvmet_allocate_nsid', $foreign, $foreign_cfs, $NQN), 1, 'others do not count');
+    my $invalid = inventory(@POOL, zfs_rows('tank/vm-1-disk-0', 'volume', $NQN, 'x', $U{1}));
+    is(nv('_nvmet_allocate_nsid', $invalid, $cfs, $NQN), 1, 'invalid NSIDs do not count');
+    my $top = inventory(
+        @POOL,
+        zfs_rows('tank/vm-1-disk-0', 'volume', $NQN, 1, $U{1}),
+        zfs_rows('tank/vm-2-disk-0', 'volume', $NQN, 0xfffffffe, $U{2}),
+    );
+    is(
+        nv('_nvmet_allocate_nsid', $top, $cfs, $NQN),
+        2,
+        'after the last NSID the lowest free one',
+    );
+    my $reserved = $rows->(1);
+    $reserved->{last_nsid} = 9;
+    is(nv('_nvmet_allocate_nsid', $reserved, $cfs, $NQN), 10, 'a reserved NSID is not reused');
+    $reserved->{last_nsid} = 0xfffffffe;
+    is(
+        nv('_nvmet_allocate_nsid', $reserved, $cfs, $NQN),
+        2,
+        'until the last NSID was handed out',
+    );
+};
+
+subtest 'export planner' => sub {
+    my $dev = '/dev/zvol/tank/vm-100-disk-0';
+    my $case = sub($namespaces) {
+        return cfs_model(subsystem => { namespaces => $namespaces });
+    };
+    my $build = build_steps(1, $U{1}, $dev);
+    my @plans = (
+        ['absent', {}, [$build], 'absent'],
+        ['present', { 1 => ns_model($U{1}, $dev) }, [], 'present'],
+        [
+            'disabled',
+            { 1 => ns_model($U{1}, $dev, '0') },
+            [[['test', '-b', $dev], write_step("$S/namespaces/1/enable", 1)]],
+            'disabled',
+        ],
+        [
+            'disabled and incomplete',
+            { 1 => ns_model($U{9}, '(null)', '0') },
+            [[['rmdir', "$S/namespaces/1"], $build->@*]],
+            'reclaim',
+        ],
+        [
+            'enabled on the template name of the zvol',
+            { 1 => ns_model($U{1}, '/dev/zvol/tank/base-100-disk-0') },
+            [[
+                write_step("$S/namespaces/1/enable", 0),
+                ['rmdir', "$S/namespaces/1"],
+                $build->@*,
+            ]],
+            'stale',
+        ],
+    );
+    for my $plan (@plans) {
+        my ($name, $namespaces, $units, $state) = $plan->@*;
+        is_deeply(
+            [nv('_nvmet_plan_export', $case->($namespaces), $NQN, 1, $U{1}, $dev)],
+            [$units, $state],
+            "plan for a namespace that is $name",
+        );
+    }
+    my $incomplete = $case->({ 1 => ns_model($U{9}, '(null)', '0') });
+    is(
+        nv('_nvmet_export_state', $incomplete, $NQN, 1, $U{1}, $dev),
+        'incomplete',
+        'a disabled namespace with another identity is incomplete',
+    );
+    is(
+        nv('_nvmet_export_state', cfs_model(no_subsystem => 1), $NQN, 1, $U{1}, $dev),
+        'nosubsys',
+        'no subsystem',
+    );
+    for my $refusal (
+        [
+            'enabled with another UUID',
+            { 1 => ns_model($U{9}, $dev) },
+            qr/NVMe namespace ID '1' has a different identity/,
+        ],
+        [
+            'enabled with another device',
+            { 1 => ns_model($U{1}, '/dev/zvol/tank/other') },
+            qr/different identity/,
+        ],
+        [
+            'enabled with another UUID on the template name',
+            { 1 => ns_model($U{9}, '/dev/zvol/tank/base-100-disk-0') },
+            qr/different identity/,
+        ],
+        [
+            'UUID in use at another NSID',
+            { 2 => ns_model($U{1}, '/dev/zvol/tank/other') },
+            qr/namespace UUID '\Q@{[ $U{1} ]}\E' is already in use/,
+        ],
+    ) {
+        my ($name, $namespaces, $error) = $refusal->@*;
+        eval { nv('_nvmet_plan_export', $case->($namespaces), $NQN, 1, $U{1}, $dev) };
+        like($@, $error, "refuses a namespace $name");
+    }
+    eval { nv('_nvmet_plan_export', cfs_model(no_subsystem => 1), $NQN, 1, $U{1}, $dev) };
+    like($@, qr/NVMe subsystem does not exist/, 'refuses without subsystem');
+};
+
+subtest 'unexport planner' => sub {
+    my $dev = '/dev/zvol/tank/vm-100-disk-0';
+    is_deeply(
+        [nv('_nvmet_plan_unexport', cfs_model(), $NQN, $U{1})],
+        [[], undef],
+        'nothing to remove',
+    );
+    is_deeply(
+        [nv('_nvmet_plan_unexport', cfs_model(no_subsystem => 1), $NQN, $U{1})],
+        [[], undef],
+        'no subsystem',
+    );
+    my ($units, $pre) = nv(
+        '_nvmet_plan_unexport',
+        cfs_model(subsystem => { namespaces => { 4 => ns_model($U{1}, $dev) } }),
+        $NQN,
+        $U{1},
+    );
+    is_deeply(
+        $units,
+        [[write_step("$S/namespaces/4/enable", 0), ['rmdir', "$S/namespaces/4"]]],
+        'an enabled namespace is disabled, then removed',
+    );
+    ok($pre->{nsid} == 4 && $pre->{enabled}, 'and was enabled');
+    ($units, $pre) = nv(
+        '_nvmet_plan_unexport',
+        cfs_model(subsystem => { namespaces => { 4 => ns_model($U{1}, $dev, '0') } }),
+        $NQN,
+        $U{1},
+    );
+    is_deeply($units, [[['rmdir', "$S/namespaces/4"]]], 'a disabled namespace is only removed');
+    ok($pre->{nsid} == 4 && !$pre->{enabled}, 'and was disabled');
+    eval {
+        nv(
+            '_nvmet_plan_unexport',
+            cfs_model(
+                subsystem => {
+                    namespaces =>
+                        { 4 => ns_model($U{1}, $dev), 5 => ns_model($U{1}, $dev, '0') },
+                },
+            ),
+            $NQN,
+            $U{1},
+        );
+    };
+    like($@, qr/duplicate namespace UUID/, 'refuses a duplicate UUID');
+};
+
+subtest 'port lookup' => sub {
+    my $port = sub($family, $address, $service, %links) {
+        return {
+            addr_trtype => 'tcp',
+            addr_adrfam => $family,
+            addr_traddr => $address,
+            addr_trsvcid => $service,
+            links => {%links},
+        };
+    };
+    my $cfs = {
+        ports => {
+            3 => $port->('ipv4', '192.0.2.21', '4420'),
+            5 => $port->('ipv4', '192.0.2.21', '4420', $NQN => 1),
+            6 => $port->('ipv4', '192.0.2.22', '4420'),
+            7 => $port->('ipv6', '2001:db8:0:0::11', '4420'),
+            8 => { $port->('ipv4', '192.0.2.23', '4420')->%*, addr_trtype => '' },
+            9 => { $port->('ipv4', '', '4420')->%* },
+        },
+    };
+    my $find =
+        sub($nqn, @portal) { return nv('_nvmet_find_port', $cfs, $nqn, portal(@portal)) };
+    is($find->($NQN, 'ipv4', '192.0.2.21', 4420), 5, 'the linked port wins');
+    is($find->($FOREIGN_NQN, 'ipv4', '192.0.2.21', 4420), 3, 'else the lowest');
+    is($find->($NQN, 'ipv4', '192.0.2.22', 4420), 6, 'exact match');
+    ok(!defined($find->($NQN, 'ipv6', '192.0.2.22', 4420)), 'wrong family');
+    ok(!defined($find->($NQN, 'ipv4', '192.0.2.22', 4421)), 'wrong service');
+    ok(!defined($find->($NQN, 'ipv4', '192.0.2.23', 4420)), 'incomplete port');
+    is($find->($NQN, 'ipv6', '2001:db8::11', 4420), 7, 'IPv6 spellings match');
+};
+
+subtest 'activation planner' => sub {
+    my $conf = { nqn => $NQN, portals => $PORTALS, hostnqns => [@HOSTS], keysha => $KEY_SHA };
+    my $inv = inventory(@POOL, @ONE);
+    my $empty = { subsystems => {}, ports => {}, hosts => {} };
+    my $dev = '/dev/zvol/tank/vm-100-disk-0';
+    my $serial = nv('_nvmet_serial', $NQN);
+
+    # a creation interrupted after the model write is completed while the subsystem is unused
+    my $unfinished = sub(%attr) {
+        return {
+            $empty->%*,
+            subsystems => {
+                $NQN => {
+                    attr_serial => '0f1e2d3c4b5a6978',
+                    attr_allow_any_host => '0',
+                    acl => {},
+                    namespaces => {},
+                    %attr,
+                },
+            },
+        };
+    };
+    is_deeply(
+        nv(
+            '_nvmet_plan_activation',
+            $inv,
+            $unfinished->(attr_model => $MODEL, attr_allow_any_host => '1'),
+            $conf,
+        )->{prepublish}->[0],
+        [write_step("$S/attr_serial", $serial), write_step("$S/attr_allow_any_host", 0)],
+        'an unused subsystem with our model is completed and denies unknown hosts',
+    );
+    for my $case (
+        [
+            'a namespace',
+            sub($c) { $c->{subsystems}->{$NQN}->{namespaces}->{1} = ns_model($U{1}, $dev) },
+        ],
+        ['an ACL', sub($c) { $c->{subsystems}->{$NQN}->{acl}->{ $HOSTS[0] } = 1 }],
+        [
+            'a port link',
+            sub($c) {
+                $c->{ports}->{7} = {
+                    addr_trtype => 'tcp',
+                    addr_adrfam => 'ipv4',
+                    addr_traddr => '192.0.2.99',
+                    addr_trsvcid => '4420',
+                    links => { $NQN => 1 },
+                };
+            },
+        ],
+    ) {
+        my ($name, $change) = $case->@*;
+        my $used = $unfinished->(attr_model => $MODEL);
+        $change->($used);
+        eval { nv('_nvmet_plan_activation', $inv, $used, $conf) };
+        like(
+            $@,
+            qr/refusing to take over existing NVMe subsystem/,
+            "a subsystem with our model and another serial and $name is refused",
+        );
+    }
+    # the identities of every owned volume are validated before any unit
+    for my $case (
+        ['a duplicate NSID', [$NQN, 1, $U{2}], qr/duplicate NSID '1'/],
+        ['a duplicate UUID', [$NQN, 2, $U{1}], qr/duplicate namespace UUID/],
+        ['an invalid UUID', [$NQN, 2, 'bad'], qr/invalid namespace UUID/],
+    ) {
+        my ($name, $identity, $error) = $case->@*;
+        my $bad =
+            inventory(@POOL, @ONE, zfs_rows('tank/vm-101-disk-0', 'volume', $identity->@*));
+        eval { nv('_nvmet_desired_namespaces', $bad, $NQN) };
+        like($@, $error, "$name is refused");
+    }
+
+    my $state = sub(%o) {
+        my $cfs = cfs_model(
+            subsystem => {
+                acl => { map { $_ => 1 } @HOSTS },
+                namespaces => { 1 => ns_model($U{1}, $dev) },
+                ($o{subsystem} // {})->%*,
+            },
+        );
+        $cfs->{ports} = {
+            map {
+                $_ => {
+                    addr_trtype => 'tcp',
+                    addr_adrfam => 'ipv4',
+                    addr_traddr => "192.0.2.2$_",
+                    addr_trsvcid => '4420',
+                    links => { $NQN => 1 },
+                }
+            } 1,
+            2,
+        };
+        $cfs->{hosts} = { map { $_ => { key_sha256 => $KEY_SHA } } @HOSTS };
+        $o{change}->($cfs) if $o{change};
+        return $cfs;
+    };
+    is_deeply(
+        nv('_nvmet_plan_activation', $inv, $state->(), $conf),
+        { prepublish => [], port_ids => [1, 2] },
+        'a converged target plans nothing',
+    );
+    is_deeply(
+        nv('_nvmet_plan_publish', $state->(), $NQN, [1, 2], [@HOSTS]),
+        [],
+        'nor publishes',
+    );
+    is_deeply(
+        nv(
+            '_nvmet_plan_activation',
+            $inv,
+            $state->(subsystem => { attr_allow_any_host => '1' }),
+            $conf,
+        )->{prepublish},
+        [[write_step("$S/attr_allow_any_host", 0)]],
+        'allow_any_host is reset',
+    );
+
+    # the key matrix for the second host
+    my $h = $HOSTS[1];
+    my $H = "$ROOT/hosts/$h";
+    my $other = sha256_hex("$KEY_B\n");
+    my $foreign_acl = sub($cfs) {
+        $cfs->{subsystems}->{$FOREIGN_NQN} = { acl => { $h => 1 }, namespaces => {} };
+    };
+    for my $case (
+        [
+            'absent',
+            sub($c) { delete $c->{hosts}->{$h} },
+            [[['mkdir', $H]], [chmod_step($H), { key => ["$H/dhchap_key"] }]],
+        ],
+        ['matching', sub($c) { }, []],
+        [
+            'different and unlinked',
+            sub($c) {
+                $c->{hosts}->{$h}->{key_sha256} = $other;
+                delete $c->{subsystems}->{$NQN}->{acl}->{$h};
+            },
+            [
+                [chmod_step($H), { key => ["$H/dhchap_key"] }],
+                [['ln', '-s', $H, "$S/allowed_hosts/$h"]],
+            ],
+        ],
+        [
+            'empty and unlinked',
+            sub($c) {
+                $c->{hosts}->{$h}->{key_sha256} = $EMPTY_KEY_SHA;
+                delete $c->{subsystems}->{$NQN}->{acl}->{$h};
+            },
+            [
+                [chmod_step($H), { key => ["$H/dhchap_key"] }],
+                [['ln', '-s', $H, "$S/allowed_hosts/$h"]],
+            ],
+        ],
+    ) {
+        my ($name, $change, $units) = $case->@*;
+        my $plan = nv('_nvmet_plan_activation', $inv, $state->(change => $change), $conf);
+        is_deeply($plan->{prepublish}, $units, "key of a host that is $name");
+    }
+    # the refusal names the first subsystem by NQN that still allows the host, if its name is valid
+    my $refusal = sub($who) {
+        my $head = "refusing to replace an in-use DH-HMAC-CHAP key for '$h': ";
+        return qr/\A\Q$head$who\E still allows this host\. Remove it on the target first\n\z/;
+    };
+    for my $case (
+        [
+            'different and linked by us',
+            sub($c) { $c->{hosts}->{$h}->{key_sha256} = $other },
+            $refusal->("subsystem '$NQN'"),
+        ],
+        [
+            'different and linked elsewhere',
+            sub($c) {
+                $c->{hosts}->{$h}->{key_sha256} = $other;
+                delete $c->{subsystems}->{$NQN}->{acl}->{$h};
+                $foreign_acl->($c);
+            },
+            $refusal->("subsystem '$FOREIGN_NQN'"),
+        ],
+        [
+            'different and linked by us and elsewhere',
+            sub($c) {
+                $c->{hosts}->{$h}->{key_sha256} = $other;
+                $foreign_acl->($c);
+            },
+            $refusal->("subsystem '$FOREIGN_NQN'"),
+        ],
+        [
+            'different and linked by a subsystem with an invalid name',
+            sub($c) {
+                $c->{hosts}->{$h}->{key_sha256} = $other;
+                delete $c->{subsystems}->{$NQN}->{acl}->{$h};
+                $c->{subsystems}->{'not an nqn'} = { acl => { $h => 1 }, namespaces => {} };
+            },
+            $refusal->('another subsystem'),
+        ],
+        [
+            'empty and linked',
+            sub($c) { $c->{hosts}->{$h}->{key_sha256} = $EMPTY_KEY_SHA },
+            $refusal->("subsystem '$NQN'"),
+        ],
+        [
+            'without a digest',
+            sub($c) { $c->{hosts}->{$h}->{key_sha256} = undef },
+            qr/does not expose a DH-HMAC-CHAP key for host/,
+        ],
+    ) {
+        my ($name, $change, $error) = $case->@*;
+        eval { nv('_nvmet_plan_activation', $inv, $state->(change => $change), $conf) };
+        like($@, $error, "refuses the key of a host that is $name");
+    }
+
+    my $undesired = $state->(
+        change => sub($c) {
+            $c->{subsystems}->{$NQN}->{namespaces}->{7} =
+                ns_model($U{7}, '/dev/zvol/tank/gone');
+            $c->{subsystems}->{$NQN}->{namespaces}->{8} = ns_model($U{8}, '(null)', '0');
+        },
+    );
+    is_deeply(
+        nv('_nvmet_plan_activation', $inv, $undesired, $conf)->{prepublish},
+        [
+            [write_step("$S/namespaces/7/enable", 0), ['rmdir', "$S/namespaces/7"]],
+            [['rmdir', "$S/namespaces/8"]],
+        ],
+        'undesired namespaces are disabled and removed',
+    );
+};
+
+subtest 'publish planner' => sub {
+    my $cfs = cfs_model(subsystem => { acl => { map { $_ => 1 } @HOSTS } });
+    $cfs->{ports} = {
+        1 => { links => { $NQN => 1 } },
+        2 => { links => {} },
+        5 => { links => { $NQN => 1, $FOREIGN_NQN => 1 } },
+        6 => { links => { $FOREIGN_NQN => 1 } },
+    };
+    my @guards = (
+        ['grep', '-qx', '0', "$S/attr_allow_any_host"],
+        map { ['test', '-L', "$S/allowed_hosts/$_"] } @HOSTS,
+    );
+    is_deeply(
+        nv('_nvmet_plan_publish', $cfs, $NQN, [1, 2], [@HOSTS]),
+        [
+            {
+                port => 2,
+                action => 'link',
+                steps => [@guards, ['ln', '-s', $S, "$ROOT/ports/2/subsystems/$NQN"]],
+            },
+            {
+                port => 5,
+                action => 'unlink',
+                steps => [['rm', "$ROOT/ports/5/subsystems/$NQN"]],
+            },
+        ],
+        'links a missing port behind the guards and unlinks a stray port',
+    );
+    $cfs->{ports}->{2}->{links}->{$NQN} = 1;
+    delete $cfs->{ports}->{5};
+    is_deeply(nv('_nvmet_plan_publish', $cfs, $NQN, [1, 2], [@HOSTS]), [], 'converged');
+};
+
+subtest 'template name and chunking' => sub {
+    is(nv('_nvmet_template_name', 'tank/vm-100-disk-0'), 'tank/base-100-disk-0', 'vm to base');
+    is(nv('_nvmet_template_name', 'tank/sub/vm-1-disk-2'), 'tank/sub/base-1-disk-2', 'nested');
+    for my $name ('tank/base-100-disk-0', 'tank/subvol-100-disk-0', 'tank/xvm-1-disk-0') {
+        eval { nv('_nvmet_template_name', $name) };
+        like($@, qr/only VM zvols can become templates/, "refuses $name");
+    }
+
+    my @units = map { build_steps($_, $U{1}, "/dev/zvol/tank/vm-$_-disk-0") } 1 .. 400;
+    my $chunks = nv('_nvmet_chunk', \@units);
+    ok($chunks->@* > 1, 'large plans are split');
+    ok(
+        !grep({ length(nv('_nvmet_render', $_)) > 65536 } $chunks->@*),
+        'no call exceeds 64 KiB',
+    );
+    ok(!grep({ $_->[0]->[0] ne 'test' || $_->@* % 6 } $chunks->@*), 'chunks hold whole units');
+    is_deeply([map { $_->@* } $chunks->@*], [map { $_->@* } @units], 'order is preserved');
+    is_deeply(nv('_nvmet_chunk', []), [], 'no units, no calls');
+    my $small = nv(
+        '_nvmet_chunk',
+        [@units[0 .. 3]],
+        length(nv('_nvmet_render', [map { $_->@* } @units[0 .. 1]])),
+    );
+    is(scalar($small->@*), 2, 'the limit is configurable');
+    eval { nv('_nvmet_chunk', [$units[0]], 10) };
+    like($@, qr/NVMe target command too long/, 'a unit over the limit is refused');
+    my $key_unit = [{ key => ["$ROOT/hosts/$HOSTS[0]/dhchap_key"] }];
+    my $mixed = nv('_nvmet_chunk', [@units[0 .. 199], $key_unit, @units[200 .. 399]]);
+    is(
+        scalar(
+            grep {
+                grep { ref($_) eq 'HASH' && $_->{key} } $_->@*
+            } $mixed->@*
+        ),
+        1,
+        'the key step is in exactly one call',
+    );
+};
+
 done_testing();
-- 
2.54.0.windows.1




  parent reply	other threads:[~2026-11-26 15:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 15:09 [PATCH storage v5 00/13] add ZFS over NVMe/TCP storage plugin Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 01/13] zfsnvme: add target state parsing and planning Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 02/13] zfsnvme: add the target transactions Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 03/13] zfsnvme: add storage configuration and volume methods Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 04/13] zfsnvme: connect the nodes and register the plugin Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 05/13] api: san luns: count zfsnvme storages as owners of their subsystem Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 06/13] api: config: check the cluster version before the first zfsnvme storage Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 07/13] test: zfsnvme: add configuration and volume tests Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 08/13] test: zfsnvme: add node side tests Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 09/13] test: zfsnvme: add an in-memory nvmet target Joaquin Varela
2026-11-26 15:09 ` Joaquin Varela [this message]
2026-11-26 15:09 ` [PATCH storage v5 11/13] test: zfsnvme: test the target flows Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 12/13] test: zfsnvme: add a fault sweep, renderer and taint tests Joaquin Varela
2026-11-26 15:09 ` [PATCH storage v5 13/13] zfsnvme: wait up to 30 seconds for the shared storage lock Joaquin Varela
2026-11-26 15:10 ` [PATCH docs v5] storage: document ZFS over NVMe/TCP Joaquin Varela
2026-11-26 15:11 ` [PATCH manager v5] ui: storage: add ZFS over NVMe/TCP editor Joaquin Varela

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126150956.310593-11-joaquinvarela@neatech.ar \
    --to=joaquinvarela@neatech.ar \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal