2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2017-03-14 21:11:05 +00:00
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
2021-08-12 16:01:39 +00:00
|
|
|
* Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
2017-03-14 21:11:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NVMe over Fabrics discovery service
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2017-03-14 21:11:05 +00:00
|
|
|
|
|
|
|
#include "nvmf_internal.h"
|
|
|
|
#include "transport.h"
|
|
|
|
|
|
|
|
#include "spdk/string.h"
|
|
|
|
#include "spdk/trace.h"
|
|
|
|
#include "spdk/nvmf_spec.h"
|
2021-08-12 16:01:39 +00:00
|
|
|
#include "spdk_internal/assert.h"
|
2017-03-14 21:11:05 +00:00
|
|
|
|
2020-10-06 16:16:26 +00:00
|
|
|
#include "spdk/log.h"
|
2017-03-14 21:11:05 +00:00
|
|
|
|
2020-10-26 10:08:32 +00:00
|
|
|
void
|
|
|
|
nvmf_update_discovery_log(struct spdk_nvmf_tgt *tgt, const char *hostnqn)
|
|
|
|
{
|
|
|
|
struct spdk_nvmf_subsystem *discovery_subsystem;
|
|
|
|
struct spdk_nvmf_ctrlr *ctrlr;
|
|
|
|
|
|
|
|
tgt->discovery_genctr++;
|
|
|
|
discovery_subsystem = spdk_nvmf_tgt_find_subsystem(tgt, SPDK_NVMF_DISCOVERY_NQN);
|
|
|
|
|
|
|
|
if (discovery_subsystem) {
|
|
|
|
/** There is a change in discovery log for hosts with given hostnqn */
|
|
|
|
TAILQ_FOREACH(ctrlr, &discovery_subsystem->ctrlrs, link) {
|
|
|
|
if (hostnqn == NULL || strcmp(hostnqn, ctrlr->hostnqn) == 0) {
|
2022-01-14 18:01:12 +00:00
|
|
|
spdk_thread_send_msg(ctrlr->thread, nvmf_ctrlr_async_event_discovery_log_change_notice, ctrlr);
|
2020-10-26 10:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 16:01:39 +00:00
|
|
|
static bool
|
|
|
|
nvmf_discovery_compare_trtype(const struct spdk_nvme_transport_id *trid1,
|
|
|
|
const struct spdk_nvme_transport_id *trid2)
|
|
|
|
{
|
|
|
|
if (trid1->trtype == SPDK_NVME_TRANSPORT_CUSTOM) {
|
|
|
|
return strcasecmp(trid1->trstring, trid2->trstring) == 0;
|
|
|
|
} else {
|
|
|
|
return trid1->trtype == trid2->trtype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
nvmf_discovery_compare_tr_addr(const struct spdk_nvme_transport_id *trid1,
|
|
|
|
const struct spdk_nvme_transport_id *trid2)
|
|
|
|
{
|
|
|
|
return trid1->adrfam == trid2->adrfam && strcasecmp(trid1->traddr, trid2->traddr) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
nvmf_discovery_compare_tr_svcid(const struct spdk_nvme_transport_id *trid1,
|
|
|
|
const struct spdk_nvme_transport_id *trid2)
|
|
|
|
{
|
|
|
|
return strcasecmp(trid1->trsvcid, trid2->trsvcid) == 0;
|
|
|
|
}
|
|
|
|
|
2019-08-29 21:54:46 +00:00
|
|
|
static struct spdk_nvmf_discovery_log_page *
|
2021-08-12 16:01:39 +00:00
|
|
|
nvmf_generate_discovery_log(struct spdk_nvmf_tgt *tgt, const char *hostnqn, size_t *log_page_size,
|
|
|
|
struct spdk_nvme_transport_id *cmd_source_trid)
|
2017-03-14 21:11:05 +00:00
|
|
|
{
|
|
|
|
uint64_t numrec = 0;
|
|
|
|
struct spdk_nvmf_subsystem *subsystem;
|
2020-02-15 05:47:36 +00:00
|
|
|
struct spdk_nvmf_subsystem_listener *listener;
|
2017-03-14 21:11:05 +00:00
|
|
|
struct spdk_nvmf_discovery_log_page_entry *entry;
|
|
|
|
struct spdk_nvmf_discovery_log_page *disc_log;
|
|
|
|
size_t cur_size;
|
2017-08-29 20:03:13 +00:00
|
|
|
uint32_t sid;
|
2017-03-14 21:11:05 +00:00
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(nvmf, "Generating log page for genctr %" PRIu64 "\n",
|
2017-08-21 20:58:04 +00:00
|
|
|
tgt->discovery_genctr);
|
2017-03-14 21:11:05 +00:00
|
|
|
|
|
|
|
cur_size = sizeof(struct spdk_nvmf_discovery_log_page);
|
|
|
|
disc_log = calloc(1, cur_size);
|
|
|
|
if (disc_log == NULL) {
|
|
|
|
SPDK_ERRLOG("Discovery log page memory allocation error\n");
|
2019-08-29 21:54:46 +00:00
|
|
|
return NULL;
|
2017-03-14 21:11:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 20:19:09 +00:00
|
|
|
for (sid = 0; sid < tgt->max_subsystems; sid++) {
|
2017-08-29 20:03:13 +00:00
|
|
|
subsystem = tgt->subsystems[sid];
|
2018-12-14 01:57:55 +00:00
|
|
|
if ((subsystem == NULL) ||
|
|
|
|
(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) ||
|
|
|
|
(subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) {
|
2017-08-29 20:03:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-18 08:40:33 +00:00
|
|
|
if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-18 21:43:15 +00:00
|
|
|
for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL;
|
|
|
|
listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) {
|
nvmf: fix trid comparison for discovery subsystem entries
When generating a discovery log page, we will add entries
for the discovery subsystem for all listeners except the
one associated with the controller that generated the
log page command. We do this comparison using
spdk_nvme_transport_id_compare().
But this function compares the subnqn of the trid, and
the subnqn is not set in either of the trids that we
are comparing.
The listener's trid always has an empty subnqn, but
the source trid has an uninitialized subnqn when
we do the comparison. This means that sometimes the
subnqn may be empty (which always happens in debug
builds) but sometimes may contain garbage. This
means that sometimes an entry would be added to the
log, even for the trid of the discovery controller
that generated the command (meaning the discovery
controller would end up referring to itself which
is not allowed).
There is an even more subtle issue with this. If the
host reads just the log page header, the nvmf target
generates the entire log page, and just returns the
header contents. Let's say in this case, the source
trid has an empty subnqn, so we don't generate an entry
for it, and report numrec = X and genctr = Y. Then
the host reads the X log page entries. But now the
source trid is garbage, so a discovery log page entry
is returned, replacing one of the "real" log page
entries. And since genctr didn't change, the host
thinks the data is all valid, meaning there's a log
page entry for an NVM subsystem that ends up getting
dropped.
Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: I96cfc566ddaf17153aec089bf3d9b3480bec3e4b
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11933
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
2022-03-10 18:33:48 +00:00
|
|
|
if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
|
|
|
|
struct spdk_nvme_transport_id source_trid = *cmd_source_trid;
|
|
|
|
struct spdk_nvme_transport_id listener_trid = *listener->trid;
|
|
|
|
|
2022-02-10 11:22:09 +00:00
|
|
|
/* Do not generate an entry for the transport ID for the listener
|
nvmf: fix trid comparison for discovery subsystem entries
When generating a discovery log page, we will add entries
for the discovery subsystem for all listeners except the
one associated with the controller that generated the
log page command. We do this comparison using
spdk_nvme_transport_id_compare().
But this function compares the subnqn of the trid, and
the subnqn is not set in either of the trids that we
are comparing.
The listener's trid always has an empty subnqn, but
the source trid has an uninitialized subnqn when
we do the comparison. This means that sometimes the
subnqn may be empty (which always happens in debug
builds) but sometimes may contain garbage. This
means that sometimes an entry would be added to the
log, even for the trid of the discovery controller
that generated the command (meaning the discovery
controller would end up referring to itself which
is not allowed).
There is an even more subtle issue with this. If the
host reads just the log page header, the nvmf target
generates the entire log page, and just returns the
header contents. Let's say in this case, the source
trid has an empty subnqn, so we don't generate an entry
for it, and report numrec = X and genctr = Y. Then
the host reads the X log page entries. But now the
source trid is garbage, so a discovery log page entry
is returned, replacing one of the "real" log page
entries. And since genctr didn't change, the host
thinks the data is all valid, meaning there's a log
page entry for an NVM subsystem that ends up getting
dropped.
Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: I96cfc566ddaf17153aec089bf3d9b3480bec3e4b
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11933
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
2022-03-10 18:33:48 +00:00
|
|
|
* entry associated with the discovery controller that generated
|
|
|
|
* this command. We compare a copy of the trids, since the trids
|
|
|
|
* here don't contain the subnqn, and the transport_id_compare()
|
|
|
|
* function will compare the subnqns.
|
2022-02-10 11:22:09 +00:00
|
|
|
*/
|
nvmf: fix trid comparison for discovery subsystem entries
When generating a discovery log page, we will add entries
for the discovery subsystem for all listeners except the
one associated with the controller that generated the
log page command. We do this comparison using
spdk_nvme_transport_id_compare().
But this function compares the subnqn of the trid, and
the subnqn is not set in either of the trids that we
are comparing.
The listener's trid always has an empty subnqn, but
the source trid has an uninitialized subnqn when
we do the comparison. This means that sometimes the
subnqn may be empty (which always happens in debug
builds) but sometimes may contain garbage. This
means that sometimes an entry would be added to the
log, even for the trid of the discovery controller
that generated the command (meaning the discovery
controller would end up referring to itself which
is not allowed).
There is an even more subtle issue with this. If the
host reads just the log page header, the nvmf target
generates the entire log page, and just returns the
header contents. Let's say in this case, the source
trid has an empty subnqn, so we don't generate an entry
for it, and report numrec = X and genctr = Y. Then
the host reads the X log page entries. But now the
source trid is garbage, so a discovery log page entry
is returned, replacing one of the "real" log page
entries. And since genctr didn't change, the host
thinks the data is all valid, meaning there's a log
page entry for an NVM subsystem that ends up getting
dropped.
Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: I96cfc566ddaf17153aec089bf3d9b3480bec3e4b
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11933
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
2022-03-10 18:33:48 +00:00
|
|
|
source_trid.subnqn[0] = '\0';
|
|
|
|
listener_trid.subnqn[0] = '\0';
|
|
|
|
if (!spdk_nvme_transport_id_compare(&listener_trid, &source_trid)) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-10 11:22:09 +00:00
|
|
|
}
|
2021-08-12 16:01:39 +00:00
|
|
|
|
|
|
|
if ((tgt->discovery_filter & SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_TYPE) != 0 &&
|
|
|
|
!nvmf_discovery_compare_trtype(listener->trid, cmd_source_trid)) {
|
|
|
|
SPDK_DEBUGLOG(nvmf, "ignore listener type %d (%s) due to type mismatch\n",
|
|
|
|
listener->trid->trtype, listener->trid->trstring);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((tgt->discovery_filter & SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_ADDRESS) != 0 &&
|
|
|
|
!nvmf_discovery_compare_tr_addr(listener->trid, cmd_source_trid)) {
|
|
|
|
SPDK_DEBUGLOG(nvmf, "ignore listener addr %s due to addr mismatch\n",
|
|
|
|
listener->trid->traddr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((tgt->discovery_filter & SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_SVCID) != 0 &&
|
|
|
|
!nvmf_discovery_compare_tr_svcid(listener->trid, cmd_source_trid)) {
|
|
|
|
SPDK_DEBUGLOG(nvmf, "ignore listener svcid %s due to svcid mismatch\n",
|
|
|
|
listener->trid->trsvcid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_DEBUGLOG(nvmf, "listener %s:%s trtype %s\n", listener->trid->traddr, listener->trid->trsvcid,
|
|
|
|
listener->trid->trstring);
|
|
|
|
|
2017-03-14 21:11:05 +00:00
|
|
|
size_t new_size = cur_size + sizeof(*entry);
|
|
|
|
void *new_log_page = realloc(disc_log, new_size);
|
|
|
|
|
|
|
|
if (new_log_page == NULL) {
|
|
|
|
SPDK_ERRLOG("Discovery log page memory allocation error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
disc_log = new_log_page;
|
|
|
|
cur_size = new_size;
|
|
|
|
|
|
|
|
entry = &disc_log->entries[numrec];
|
|
|
|
memset(entry, 0, sizeof(*entry));
|
2021-12-07 23:24:27 +00:00
|
|
|
entry->portid = listener->id;
|
2017-03-14 21:11:05 +00:00
|
|
|
entry->cntlid = 0xffff;
|
2018-08-22 23:04:16 +00:00
|
|
|
entry->asqsz = listener->transport->opts.max_aq_depth;
|
2017-03-14 21:11:05 +00:00
|
|
|
entry->subtype = subsystem->subtype;
|
|
|
|
snprintf(entry->subnqn, sizeof(entry->subnqn), "%s", subsystem->subnqn);
|
|
|
|
|
2020-05-15 01:14:26 +00:00
|
|
|
nvmf_transport_listener_discover(listener->transport, listener->trid, entry);
|
2017-03-14 21:11:05 +00:00
|
|
|
|
|
|
|
numrec++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disc_log->numrec = numrec;
|
2017-08-21 20:58:04 +00:00
|
|
|
disc_log->genctr = tgt->discovery_genctr;
|
2019-08-29 21:54:46 +00:00
|
|
|
*log_page_size = cur_size;
|
2017-03-14 21:11:05 +00:00
|
|
|
|
2019-08-29 21:54:46 +00:00
|
|
|
return disc_log;
|
2017-03-14 21:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-05-09 22:57:13 +00:00
|
|
|
nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, const char *hostnqn, struct iovec *iov,
|
2021-08-12 16:01:39 +00:00
|
|
|
uint32_t iovcnt, uint64_t offset, uint32_t length,
|
|
|
|
struct spdk_nvme_transport_id *cmd_source_trid)
|
2017-03-14 21:11:05 +00:00
|
|
|
{
|
|
|
|
size_t copy_len = 0;
|
2018-11-29 21:11:31 +00:00
|
|
|
size_t zero_len = 0;
|
|
|
|
struct iovec *tmp;
|
2019-08-29 21:54:46 +00:00
|
|
|
size_t log_page_size = 0;
|
|
|
|
struct spdk_nvmf_discovery_log_page *discovery_log_page;
|
2017-03-14 21:11:05 +00:00
|
|
|
|
2021-08-12 16:01:39 +00:00
|
|
|
discovery_log_page = nvmf_generate_discovery_log(tgt, hostnqn, &log_page_size, cmd_source_trid);
|
2017-03-14 21:11:05 +00:00
|
|
|
|
|
|
|
/* Copy the valid part of the discovery log page, if any */
|
2019-08-29 21:54:46 +00:00
|
|
|
if (discovery_log_page) {
|
2018-11-29 21:11:31 +00:00
|
|
|
for (tmp = iov; tmp < iov + iovcnt; tmp++) {
|
|
|
|
copy_len = spdk_min(tmp->iov_len, length);
|
2019-08-29 21:54:46 +00:00
|
|
|
copy_len = spdk_min(log_page_size - offset, copy_len);
|
2017-03-14 21:11:05 +00:00
|
|
|
|
2019-08-29 21:54:46 +00:00
|
|
|
memcpy(tmp->iov_base, (char *)discovery_log_page + offset, copy_len);
|
2017-03-14 21:11:05 +00:00
|
|
|
|
2018-11-29 21:11:31 +00:00
|
|
|
offset += copy_len;
|
|
|
|
length -= copy_len;
|
|
|
|
zero_len = tmp->iov_len - copy_len;
|
2019-08-29 21:54:46 +00:00
|
|
|
if (log_page_size <= offset || length == 0) {
|
2018-11-29 21:11:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Zero out the rest of the payload */
|
|
|
|
if (zero_len) {
|
|
|
|
memset((char *)tmp->iov_base + copy_len, 0, zero_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (++tmp; tmp < iov + iovcnt; tmp++) {
|
|
|
|
memset((char *)tmp->iov_base, 0, tmp->iov_len);
|
|
|
|
}
|
2019-08-29 21:54:46 +00:00
|
|
|
|
|
|
|
free(discovery_log_page);
|
2018-11-29 21:11:31 +00:00
|
|
|
}
|
2017-03-14 21:11:05 +00:00
|
|
|
}
|