Commit 149a468d authored by Konrad Sztyber's avatar Konrad Sztyber Committed by Tomasz Zawadzki
Browse files

bdev/nvme: move loading PSKs



Signed-off-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I5e43e90d8e7228b9b2239645dd76d92916a3d936
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/21746


Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent 83b6f7e4
Loading
Loading
Loading
Loading
+52 −3
Original line number Diff line number Diff line
@@ -5929,6 +5929,45 @@ bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
	return true;
}

static int
bdev_nvme_load_psk(const char *fname, char *buf, size_t bufsz)
{
	FILE *psk_file;
	struct stat statbuf;
	int rc;
#define TCP_PSK_INVALID_PERMISSIONS 0177

	if (stat(fname, &statbuf) != 0) {
		SPDK_ERRLOG("Could not read permissions for PSK file\n");
		return -EACCES;
	}

	if ((statbuf.st_mode & TCP_PSK_INVALID_PERMISSIONS) != 0) {
		SPDK_ERRLOG("Incorrect permissions for PSK file\n");
		return -EPERM;
	}
	if ((size_t)statbuf.st_size >= bufsz) {
		SPDK_ERRLOG("Invalid PSK: too long\n");
		return -EINVAL;
	}
	psk_file = fopen(fname, "r");
	if (psk_file == NULL) {
		SPDK_ERRLOG("Could not open PSK file\n");
		return -EINVAL;
	}

	memset(buf, 0, bufsz);
	rc = fread(buf, 1, statbuf.st_size, psk_file);
	if (rc != statbuf.st_size) {
		SPDK_ERRLOG("Failed to read PSK\n");
		fclose(psk_file);
		return -EINVAL;
	}

	fclose(psk_file);
	return 0;
}

int
bdev_nvme_create(struct spdk_nvme_transport_id *trid,
		 const char *base_name,
@@ -5943,7 +5982,7 @@ bdev_nvme_create(struct spdk_nvme_transport_id *trid,
	struct nvme_probe_skip_entry *entry, *tmp;
	struct nvme_async_probe_ctx *ctx;
	spdk_nvme_attach_cb attach_cb;
	int len;
	int rc, len;

	/* TODO expand this check to include both the host and target TRIDs.
	 * Only if both are the same should we fail.
@@ -6006,6 +6045,16 @@ bdev_nvme_create(struct spdk_nvme_transport_id *trid,
	ctx->drv_opts.disable_read_ana_log_page = true;
	ctx->drv_opts.transport_tos = g_opts.transport_tos;

	if (ctx->bdev_opts.psk_path[0] != '\0') {
		rc = bdev_nvme_load_psk(ctx->bdev_opts.psk_path,
					ctx->drv_opts.psk, sizeof(ctx->drv_opts.psk));
		if (rc != 0) {
			SPDK_ERRLOG("Could not load PSK from %s\n", ctx->bdev_opts.psk_path);
			free(ctx);
			return rc;
		}
	}

	if (nvme_bdev_ctrlr_get_by_name(base_name) == NULL || multipath) {
		attach_cb = connect_attach_cb;
	} else {
+1 −46
Original line number Diff line number Diff line
@@ -21,8 +21,6 @@
#include "spdk/log.h"
#include "spdk/bdev_module.h"

#define TCP_PSK_INVALID_PERMISSIONS 0177

static bool g_tls_log = false;

static int
@@ -321,44 +319,6 @@ rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
	spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx);
}

static int
tcp_load_psk(const char *fname, char *buf, size_t bufsz)
{
	FILE *psk_file;
	struct stat statbuf;
	int rc;

	if (stat(fname, &statbuf) != 0) {
		SPDK_ERRLOG("Could not read permissions for PSK file\n");
		return -EACCES;
	}

	if ((statbuf.st_mode & TCP_PSK_INVALID_PERMISSIONS) != 0) {
		SPDK_ERRLOG("Incorrect permissions for PSK file\n");
		return -EPERM;
	}
	if ((size_t)statbuf.st_size >= bufsz) {
		SPDK_ERRLOG("Invalid PSK: too long\n");
		return -EINVAL;
	}
	psk_file = fopen(fname, "r");
	if (psk_file == NULL) {
		SPDK_ERRLOG("Could not open PSK file\n");
		return -EINVAL;
	}

	memset(buf, 0, bufsz);
	rc = fread(buf, 1, statbuf.st_size, psk_file);
	if (rc != statbuf.st_size) {
		SPDK_ERRLOG("Failed to read PSK\n");
		fclose(psk_file);
		return -EINVAL;
	}

	fclose(psk_file);
	return 0;
}

static void
rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request,
				const struct spdk_json_val *params)
@@ -478,12 +438,7 @@ rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request,
			SPDK_NOTICELOG("TLS support is considered experimental\n");
			g_tls_log = true;
		}
		rc = tcp_load_psk(ctx->req.psk, ctx->req.drv_opts.psk, sizeof(ctx->req.drv_opts.psk));
		if (rc) {
			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Could not retrieve PSK from file: %s",
							     ctx->req.psk);
			goto cleanup;
		}

		rc = snprintf(ctx->req.bdev_opts.psk_path, sizeof(ctx->req.bdev_opts.psk_path), "%s", ctx->req.psk);
		if (rc < 0 || (size_t)rc >= sizeof(ctx->req.bdev_opts.psk_path)) {
			spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Could not store PSK path: %s",