Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions libnvme/src/nvme/config-emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <shared/compiler-attributes-util.h>
#include <shared/fs-util.h>
#include <shared/nqn-util.h>
#include <nvme/config.h>
#include <nvme/nvme-types-fabrics.h>

Expand Down Expand Up @@ -187,6 +188,7 @@ __shr_public int libnvmf_config_emit_add(
const char *hostnqn, const char *hostid,
const struct libnvmf_params *params, const char *hostsymname)
{
__cleanup_free char *derived = NULL;
struct emit_persona *persona;
struct emit_conn *conn;
int ret;
Expand All @@ -205,6 +207,18 @@ __shr_public int libnvmf_config_emit_add(
return -EINVAL;
}

/* The reader rejects a hostnqn without a hostid */
if (hostnqn && !hostid) {
derived = libnvme_hostid_from_hostnqn(hostnqn);
if (!derived || !shr_hostid_valid(derived)) {
emit_err(emitter->ctx,
"config emit: hostnqn %s requires a hostid",
hostnqn);
return -EINVAL;
}
hostid = derived;
}

ret = check_persona_identity(emitter, hostnqn, hostid);
if (ret)
return ret;
Expand Down Expand Up @@ -460,17 +474,12 @@ static void outfiles_free(struct outfile *out, size_t n, bool rollback)
size_t i;

for (i = 0; i < n; i++) {
if (rollback) {
/*
* Roll back a partial install: each entry was either
* already renamed into place (only @final exists) or
* never got that far (only @tmp exists). Removing
* both is safe -- whichever one isn't there just
* fails with a harmless ENOENT.
*/
unlink(out[i].final);
/*
* Remove only the temp files. A final path may hold a file
* from before this install, which must not be lost.
*/
if (rollback)
unlink(out[i].tmp);
}
free(out[i].tmp);
free(out[i].final);
}
Expand Down
16 changes: 10 additions & 6 deletions libnvme/src/nvme/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,18 @@ void libnvmf_config_emit_free(struct libnvmf_config_emitter *emitter);
* @host_traddr: Host transport address, or NULL.
* @host_iface: Host interface, or NULL.
* @hostnqn: Host NQN, or NULL.
* @hostid: Host identifier, or NULL.
* @hostid: Host identifier, or NULL. If NULL and @hostnqn is set,
* the UUID of a UUID-based @hostnqn is used.
* @params: Connection parameters, or NULL.
* @hostsymname: Host symbolic name, or NULL.
*
* The parameter set is copied by the emitter.
*
* Return:
* * 0 on success.
* * -EINVAL if a required parameter is missing or if @hostsymname
* conflicts with an existing persona.
* * -EINVAL if a required parameter is missing, if @hostnqn is given
* without @hostid and is not UUID-based, or if @hostsymname conflicts
* with an existing persona.
* * -ENOMEM if memory allocation fails.
*/
int libnvmf_config_emit_add(struct libnvmf_config_emitter *emitter,
Expand All @@ -481,9 +483,11 @@ int libnvmf_config_emit_add(struct libnvmf_config_emitter *emitter,
* named persona is written to a separate drop-in file.
*
* The generated configuration is validated by reading it back through the
* standard parser before it is installed. Files are written atomically
* (temporary file followed by rename). If any step fails, no configuration
* is installed.
* standard parser before it is installed. Each file is written to a
* temporary file, and all of them are renamed into place only after every
* temporary file was written. If writing fails, existing files are left
* unchanged. If a rename fails, the files renamed before it hold the new
* configuration.
*
* Return:
* * 0 on success.
Expand Down
36 changes: 11 additions & 25 deletions libnvme/src/nvme/fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,20 +841,6 @@ __shr_public int libnvmf_context_set_connection(
return 0;
}

static const char *hostid_from_hostnqn(const char *hostnqn)
{
const char *match;

if (!hostnqn)
return NULL;

match = strstr(hostnqn, "uuid:");
if (!match)
return NULL;

return match + strlen("uuid:");
}

__shr_public int libnvmf_context_set_hostnqn(struct libnvmf_context *fctx,
const char *hostnqn, const char *hostid)
{
Expand All @@ -868,18 +854,18 @@ __shr_public int libnvmf_context_set_hostnqn(struct libnvmf_context *fctx,
if (!hnqn)
return -ENOMEM;

if (!hostid)
hostid = hostid_from_hostnqn(hostnqn);

if (!hostid) {
free(hnqn);
return -EINVAL;
}

hid = strdup(hostid);
if (!hid) {
free(hnqn);
return -ENOMEM;
hid = libnvme_hostid_from_hostnqn(hostnqn);
if (!hid) {
free(hnqn);
return -EINVAL;
}
} else {
hid = strdup(hostid);
if (!hid) {
free(hnqn);
return -ENOMEM;
}
}

free(fctx->hostnqn);
Expand Down
2 changes: 0 additions & 2 deletions libnvme/src/nvme/private-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ extern char NO_ATTR[];
"nqn.2014-08.org.nvmexpress:uuid:00000000-0000-0000-0000-000000000000"
#define NVME_DEFAULT_HOSTID "00000000-0000-0000-0000-000000000000"

char *libnvme_hostid_from_hostnqn(const char *hostnqn);

/**
* libnvme_parse_src_addr - Extract the source address from an address string
* @ctx: libnvme global context
Expand Down
124 changes: 124 additions & 0 deletions libnvme/tests/config-emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,68 @@ static bool test_force_overwrite(struct libnvme_global_ctx *ctx,
return pass;
}

/*
* A failed force install keeps the files it did not replace. The main
* file's name leaves no room for the temp-file suffix: its temp file
* cannot be created, after the drop-in's temp file was.
*/
static bool test_force_failure_keeps_files(struct libnvme_global_ctx *ctx,
struct fixture *fx)
{
char name[251], main_path[300], ddir[304], dropin[324], buf[16];
struct libnvmf_config_emitter *e;
bool pass = true;
FILE *f;

printf("test_force_failure_keeps_files:\n");

memset(name, 'c', sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
snprintf(main_path, sizeof(main_path), "%s/%s", fx->dir, name);
snprintf(ddir, sizeof(ddir), "%s.d", main_path);
snprintf(dropin, sizeof(dropin), "%s/100-persona.conf", ddir);

/* An existing configuration: a main file and one drop-in. */
shr_assert(mkdir(ddir, 0755) == 0);
f = fopen(main_path, "w");
shr_assert(f);
shr_assert(fclose(f) == 0);
f = fopen(dropin, "w");
shr_assert(f);
shr_assert(fputs("old\n", f) >= 0);
shr_assert(fclose(f) == 0);

e = libnvmf_config_emit_new(ctx);
shr_assert(e);
shr_assert(add(e, false, "10.0.0.9", "nqn.2014-08.org.nvmexpress:a",
"nqn.2014-08.org.nvmexpress:host-a", g_hostid, NULL,
NULL, NULL) == 0);
if (!libnvmf_config_emit_install(e, main_path, true)) {
printf(" - install did not fail [FAIL]\n");
pass = false;
}
libnvmf_config_emit_free(e);

f = fopen(dropin, "r");
if (!f || !fgets(buf, sizeof(buf), f) || strcmp(buf, "old\n")) {
printf(" - existing drop-in lost or changed [FAIL]\n");
pass = false;
} else if (access(main_path, F_OK)) {
printf(" - existing main file lost [FAIL]\n");
pass = false;
} else {
printf(" - failed install keeps existing files [PASS]\n");
}
if (f)
fclose(f);

unlink(dropin);
rmdir(ddir);
unlink(main_path);

return pass;
}

static bool install_one(struct libnvme_global_ctx *ctx, const char *path,
bool force)
{
Expand Down Expand Up @@ -528,6 +590,64 @@ static bool test_all_dropins(struct libnvme_global_ctx *ctx, struct fixture *fx)
return pass;
}

/* A hostnqn without a hostid takes the UUID of a UUID-based hostnqn. */
static bool test_hostid_from_hostnqn(struct libnvme_global_ctx *ctx,
struct fixture *fx)
{
struct libnvmf_config_emitter *e;
struct conn_list list = { 0 };
struct libnvmf_config *config;
const char *hostid;
char hostnqn[96];
bool pass = true;

printf("test_hostid_from_hostnqn:\n");

snprintf(hostnqn, sizeof(hostnqn),
"nqn.2014-08.org.nvmexpress:uuid:%s", g_hostid);

e = libnvmf_config_emit_new(ctx);
shr_assert(e);
shr_assert(add(e, false, "10.0.0.9", "nqn.2014-08.org.nvmexpress:a",
hostnqn, NULL, NULL, NULL, NULL) == 0);
if (libnvmf_config_emit_install(e, fx->main_path, false)) {
printf(" - install failed [FAIL]\n");
libnvmf_config_emit_free(e);
return false;
}
libnvmf_config_emit_free(e);

if (libnvmf_config_read(ctx, fx->main_path, &config)) {
printf(" - written config not readable [FAIL]\n");
return false;
}
libnvmf_config_conn_for_each(config, collect, &list);
hostid = list.n == 1 ?
libnvmf_config_conn_get_hostid(list.conn[0]) : NULL;
if (!hostid || strcmp(hostid, g_hostid)) {
printf(" - hostid not taken from the hostnqn [FAIL]\n");
pass = false;
} else {
printf(" - hostid taken from the hostnqn, read back [PASS]\n");
}
libnvmf_config_free(config);

/* No UUID in the hostnqn: nothing to take the hostid from. */
e = libnvmf_config_emit_new(ctx);
shr_assert(e);
if (add(e, false, "10.0.0.9", "nqn.v",
"nqn.2014-08.org.nvmexpress:host-a", NULL, NULL,
NULL, NULL) != -EINVAL) {
printf(" - non-UUID hostnqn without hostid accepted [FAIL]\n");
pass = false;
} else {
printf(" - non-UUID hostnqn without hostid rejected [PASS]\n");
}
libnvmf_config_emit_free(e);

return pass;
}

static bool test_add_validation(struct libnvme_global_ctx *ctx)
{
struct libnvmf_config_emitter *e;
Expand Down Expand Up @@ -660,10 +780,14 @@ int main(void)
fixture_wipe(&fx);
pass &= test_force_overwrite(ctx, &fx);
fixture_wipe(&fx);
pass &= test_force_failure_keeps_files(ctx, &fx);
fixture_wipe(&fx);
pass &= test_file_mode(ctx, &fx);
fixture_wipe(&fx);
pass &= test_all_dropins(ctx, &fx);
fixture_wipe(&fx);
pass &= test_hostid_from_hostnqn(ctx, &fx);
fixture_wipe(&fx);
pass &= test_add_validation(ctx);
pass &= test_params_set_validation();

Expand Down
18 changes: 9 additions & 9 deletions libnvme/tests/test-fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ static bool test_sanitize_discovery_log_entry(struct libnvme_global_ctx *ctx)
}

/* -------------------------------------------------------------------------
* hostid_from_hostnqn — extract UUID from a hostnqn string
* libnvme_hostid_from_hostnqn — extract UUID from a hostnqn string
* -------------------------------------------------------------------------
*/
static bool test_hostid_from_hostnqn(void)
{
const char *id;
char *id;
bool pass = true;

printf("\ntest_hostid_from_hostnqn:\n");

id = hostid_from_hostnqn(NULL);
pass = (id == NULL);
CHECK(pass, "NULL input → NULL");

id = hostid_from_hostnqn("nqn.2014-08.org.nvmexpress:no-uuid-here");
id = libnvme_hostid_from_hostnqn(
"nqn.2014-08.org.nvmexpress:no-uuid-here");
pass = (id == NULL);
CHECK(pass, "no uuid: tag → NULL");
free(id);

id = hostid_from_hostnqn(
id = libnvme_hostid_from_hostnqn(
"nqn.2014-08.org.nvmexpress:uuid:9ba1651a-ed36-11f0-9858-6c1ff71ba506");
pass = (id && !strcmp(id, "9ba1651a-ed36-11f0-9858-6c1ff71ba506"));
CHECK(pass, "valid NQN → UUID \"%s\"", id ? id : "(null)");
free(id);

id = hostid_from_hostnqn("prefix:uuid:abc-123");
id = libnvme_hostid_from_hostnqn("prefix:uuid:abc-123");
pass = (id && !strcmp(id, "abc-123"));
CHECK(pass, "arbitrary prefix → \"abc-123\"");
free(id);

return pass;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/cli/nvme_config_convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def test_convert_dc_and_subsystem(self):
def test_convert_legacy_bare_array_format(self):
self._write_json([{
'hostnqn': 'nqn.2014-08.org.nvmexpress:2222',
'hostid': '22222222-2222-4222-8222-222222222222',
'subsystems': [{
'nqn': 'nqn.2024-01.com.example:data.vol2',
'ports': [{
Expand All @@ -169,6 +170,7 @@ def test_dhchap_default_inherited_by_port(self):
self._write_json({
'hosts': [{
'hostnqn': 'nqn.2014-08.org.nvmexpress:3333',
'hostid': '33333333-3333-4333-8333-333333333333',
'dhchap_key': 'DHHC-1:00:host-default-key:',
'subsystems': [{
'nqn': 'nqn.2024-01.com.example:data.vol3',
Expand All @@ -187,6 +189,7 @@ def test_dhchap_port_value_overrides_default(self):
self._write_json({
'hosts': [{
'hostnqn': 'nqn.2014-08.org.nvmexpress:4444',
'hostid': '44444444-4444-4444-8444-444444444444',
'dhchap_key': 'DHHC-1:00:host-default-key:',
'subsystems': [{
'nqn': 'nqn.2024-01.com.example:data.vol4',
Expand Down Expand Up @@ -301,6 +304,23 @@ def test_malformed_json_errors(self):
self._convert(expect_fail=True)
self.assertFalse(os.path.exists(self.output_ini))

def test_hostnqn_without_hostid_is_skipped(self):
self._write_json({
'hosts': [{
'hostnqn': 'nqn.2014-08.org.nvmexpress:5555',
'subsystems': [{
'nqn': 'nqn.2024-01.com.example:data.vol5',
'ports': [{
'transport': 'tcp',
'traddr': '10.0.0.8',
}],
}],
}],
})
result = self._convert()
self.assertIn('requires a hostid', result.stderr)
self.assertNotIn('data.vol5', self._read_output())

def test_missing_hosts_array_errors(self):
self._write_json({'not-hosts': []})
self._convert(expect_fail=True)
Expand Down
Loading