Commit e2d54380 authored by kyuho.son's avatar kyuho.son Committed by Jim Harris
Browse files

nvmf: Add support for mDNS Pull Registration Requests (TP-8009, TP-8010a, and TP-8024)



To enable the SPDK NVMe-oF target discovery controller to act as a DDC, this patch added support for mDNS Pull Registration Requests. A DDC may respond to mDNS queries for the service name of "._nvme-disc._<protocol>.local." This patch covers the sending part of the mDNS message.

Change-Id: I3f759341c269a14d1a008e5577132880796d3d74
Signed-off-by: default avatarkyuho.son <kyuho.son@samsung.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/23036


Community-CI: Mellanox Build Bot
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
parent 985ef53a
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -13213,3 +13213,37 @@ Example response:
  "result": true
}
~~~

### nvmf_publish_mdns_prr {#rpc_nvmf_publish_mdns_prr}

This interface is used to publish an NVMf target's service location using mDNS
(Multicast DNS) protocol. It allows clients to discover the NVMf target using
the published service location.

#### Parameters

Name                       | Optional | Type        | Description
-------------------------- | -------- | ----------- | -----------
tgt_name                   | Optional | string      | Parent NVMe-oF target name.

#### Example

Example request:

~~~json
{
  "jsonrpc": "2.0",
  "method": "nvmf_publish_mdns_prr",
  "id": 1,
}
~~~

Example response:

~~~json
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": true
}
~~~
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ SO_MINOR := 0

C_SRCS = ctrlr.c ctrlr_discovery.c ctrlr_bdev.c \
	 subsystem.c nvmf.c nvmf_rpc.c transport.c tcp.c \
	 stubs.c
	 stubs.c mdns_server.c

C_SRCS-$(CONFIG_RDMA) += rdma.c
C_SRCS-$(CONFIG_HAVE_EVP_MAC) += auth.c

lib/nvmf/mdns_server.c

0 → 100644
+244 −0
Original line number Diff line number Diff line
/*  SPDX-License-Identifier: BSD-3-Clause
 *  Copyright (c) 2022 Dell Inc, or its subsidiaries.
 *  Copyright (c) 2024 Samsung Electronics Co., Ltd. All rights reserved.
 *  All rights reserved.
 */

#include "spdk/stdinc.h"
#include "spdk/env.h"
#include "spdk/thread.h"
#include "nvmf_internal.h"
#include "spdk/log.h"
#include "spdk/config.h"
#include "spdk/nvme.h"
#include "spdk/string.h"

#ifdef SPDK_CONFIG_AVAHI
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-client/lookup.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>

#define NVMF_MAX_DNS_NAME_LENGTH 255

static AvahiSimplePoll *g_avahi_publish_simple_poll = NULL;
static AvahiClient *g_avahi_publish_client = NULL;
static AvahiEntryGroup *g_avahi_entry_group = NULL;

struct mdns_publish_ctx {
	bool				stop;
	struct spdk_poller		*poller;
	struct spdk_nvmf_subsystem	*subsystem;
	struct spdk_nvmf_tgt		*tgt;
};

static struct mdns_publish_ctx *g_mdns_publish_ctx = NULL;

static void
nvmf_avahi_publish_destroy(struct mdns_publish_ctx *ctx)
{
	if (g_avahi_entry_group) {
		avahi_entry_group_free(g_avahi_entry_group);
		g_avahi_entry_group = NULL;
	}

	if (g_avahi_publish_client) {
		avahi_client_free(g_avahi_publish_client);
		g_avahi_publish_client = NULL;
	}

	if (g_avahi_publish_simple_poll) {
		avahi_simple_poll_free(g_avahi_publish_simple_poll);
		g_avahi_publish_simple_poll = NULL;
	}

	g_mdns_publish_ctx = NULL;
	free(ctx);
}

static int
nvmf_avahi_publish_iterate(void *arg)
{
	struct mdns_publish_ctx *ctx = arg;
	int rc;

	if (ctx == NULL) {
		assert(false);
		return SPDK_POLLER_IDLE;
	}

	if (ctx->stop) {
		SPDK_INFOLOG(nvmf, "Stopping avahi publish poller\n");
		spdk_poller_unregister(&ctx->poller);
		nvmf_avahi_publish_destroy(ctx);
		return SPDK_POLLER_BUSY;
	}

	rc = avahi_simple_poll_iterate(g_avahi_publish_simple_poll, 0);
	if (rc && rc != -EAGAIN) {
		SPDK_ERRLOG("avahi publish poll returned error\n");
		spdk_poller_unregister(&ctx->poller);
		nvmf_avahi_publish_destroy(ctx);
		return SPDK_POLLER_BUSY;
	}

	return SPDK_POLLER_BUSY;
}

static void
nvmf_ctx_stop_mdns_prr(struct mdns_publish_ctx *ctx)
{
	ctx->stop = true;
}

static int
publish_pull_registration_request(AvahiClient *client, struct mdns_publish_ctx *publish_ctx)
{
	struct spdk_nvmf_subsystem *subsystem = publish_ctx->subsystem;
	struct spdk_nvmf_subsystem_listener *listener;
	const char *name_base = "spdk";
	const char *type_base = "_nvme-disc";
	const char *domain = "local";
	char *protocol;
	char name[NVMF_MAX_DNS_NAME_LENGTH];
	char type[NVMF_MAX_DNS_NAME_LENGTH];
	char txt_protocol[NVMF_MAX_DNS_NAME_LENGTH];
	char txt_nqn[NVMF_MAX_DNS_NAME_LENGTH];
	AvahiStringList *txt = NULL;
	uint16_t port;
	uint16_t id = 0;

	if (g_avahi_entry_group != NULL) {
		return 0;
	}

	g_avahi_entry_group = avahi_entry_group_new(client, NULL, NULL);
	if (g_avahi_entry_group == NULL) {
		SPDK_ERRLOG("avahi_entry_group_new failure: %s\n", avahi_strerror(avahi_client_errno(client)));
		return -1;
	}

	TAILQ_FOREACH(listener, &subsystem->listeners, link) {
		if (listener->trid->trtype == SPDK_NVME_TRANSPORT_TCP) {
			protocol = "tcp";
		} else if (listener->trid->trtype == SPDK_NVME_TRANSPORT_RDMA) {
			SPDK_ERRLOG("Current SPDK doesn't distinguish RoCE(udp) and iWARP(tcp). Skip adding listener id %d to avahi entry",
				    listener->id);
			continue;
		} else {
			SPDK_ERRLOG("mDNS PRR does not support trtype %d", listener->trid->trtype);
			continue;
		}

		snprintf(type, sizeof(type), "%s._%s", type_base, protocol);
		snprintf(name, sizeof(name), "%s%d", name_base, id++);
		snprintf(txt_protocol, sizeof(txt_protocol), "p=%s", protocol);
		snprintf(txt_nqn, sizeof(txt_nqn), "nqn=%s", SPDK_NVMF_DISCOVERY_NQN);
		txt = avahi_string_list_add(txt, txt_protocol);
		txt = avahi_string_list_add(txt, txt_nqn);
		port = spdk_strtol(listener->trid->trsvcid, 10);

		if (avahi_entry_group_add_service_strlst(g_avahi_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
				0, name, type, domain, NULL, port, txt) < 0) {
			SPDK_ERRLOG("Failed to add avahi service name: %s, type: %s, domain: %s, port: %d, error: %s\n",
				    name, type, domain, port, avahi_strerror(avahi_client_errno(client)));
			continue;
		}
	}

	avahi_entry_group_commit(g_avahi_entry_group);

	return 0;
}

static void
publish_client_new_callback(AvahiClient *client, AvahiClientState avahi_state,
			    AVAHI_GCC_UNUSED void *user_data)
{
	int rc;
	struct mdns_publish_ctx *publish_ctx = user_data;

	switch (avahi_state) {
	case AVAHI_CLIENT_S_RUNNING:
		rc = publish_pull_registration_request(client, publish_ctx);
		if (rc) {
			nvmf_ctx_stop_mdns_prr(publish_ctx);
		}
		break;
	case AVAHI_CLIENT_CONNECTING:
		SPDK_INFOLOG(nvmf, "Avahi client waiting for avahi-daemon");
		break;
	case AVAHI_CLIENT_S_REGISTERING:
		SPDK_INFOLOG(nvmf, "Avahi client registering service");
		break;
	case AVAHI_CLIENT_FAILURE:
		SPDK_ERRLOG("Server connection failure: %s\n", avahi_strerror(avahi_client_errno(client)));
		nvmf_ctx_stop_mdns_prr(publish_ctx);
		break;
	case AVAHI_CLIENT_S_COLLISION:
		SPDK_ERRLOG("Avahi client name is already used in the mDNS");
		nvmf_ctx_stop_mdns_prr(publish_ctx);
		break;
	default:
		SPDK_ERRLOG("Avahi client is in unsupported state");
		break;
	}
}

int
nvmf_publish_mdns_prr(struct spdk_nvmf_tgt *tgt)
{
	int error;
	struct mdns_publish_ctx *publish_ctx = NULL;
	struct spdk_nvmf_subsystem *subsystem = NULL;

	if (g_mdns_publish_ctx != NULL) {
		if (g_mdns_publish_ctx->tgt == tgt) {
			SPDK_ERRLOG("mDNS server is already running on target %s.\n", tgt->name);
			return -EEXIST;
		}
		SPDK_ERRLOG("mDNS server does not support publishing multiple targets simultaneously.");
		return -EINVAL;
	}

	subsystem = spdk_nvmf_tgt_find_subsystem(tgt, SPDK_NVMF_DISCOVERY_NQN);
	if (TAILQ_EMPTY(&subsystem->listeners)) {
		SPDK_ERRLOG("Discovery subsystem has no listeners.\n");
		return -EINVAL;
	}

	publish_ctx = calloc(1, sizeof(*publish_ctx));
	if (publish_ctx == NULL) {
		SPDK_ERRLOG("Error creating mDNS publish ctx\n");
		return -ENOMEM;
	}
	publish_ctx->subsystem = subsystem;
	publish_ctx->tgt = tgt;
	/* Allocate main loop object */
	g_avahi_publish_simple_poll = avahi_simple_poll_new();
	if (g_avahi_publish_simple_poll == NULL) {
		SPDK_ERRLOG("Failed to create poll object for mDNS publish.\n");
		nvmf_avahi_publish_destroy(publish_ctx);
		return -ENOMEM;
	}

	assert(g_avahi_publish_client == NULL);

	/* Allocate a new client */
	g_avahi_publish_client = avahi_client_new(avahi_simple_poll_get(g_avahi_publish_simple_poll),
				 0, publish_client_new_callback, publish_ctx, &error);
	/* Check whether creating the client object succeeded */
	if (g_avahi_publish_client == NULL) {
		SPDK_ERRLOG("Failed to create mDNS client Error: %s\n", avahi_strerror(error));
		nvmf_avahi_publish_destroy(publish_ctx);
		return -ENOMEM;
	}

	g_mdns_publish_ctx = publish_ctx;
	publish_ctx->poller = SPDK_POLLER_REGISTER(nvmf_avahi_publish_iterate, publish_ctx, 100 * 1000);
	return 0;
}

#endif
+9 −0
Original line number Diff line number Diff line
@@ -576,4 +576,13 @@ int nvmf_bdev_ctrlr_zcopy_start(struct spdk_bdev *bdev,
 */
void nvmf_bdev_ctrlr_zcopy_end(struct spdk_nvmf_request *req, bool commit);

/**
 * Publishes the mDNS PRR (Pull Registration Request) for the NVMe-oF target.
 *
 * \param tgt The NVMe-oF target
 *
 * \return 0 on success, negative errno on failure
 */
int nvmf_publish_mdns_prr(struct spdk_nvmf_tgt *tgt);

#endif /* __NVMF_INTERNAL_H__ */
+53 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
#include "spdk/string.h"
#include "spdk/util.h"
#include "spdk/bit_array.h"
#include "spdk/config.h"

#include "spdk_internal/assert.h"

@@ -2985,3 +2986,55 @@ rpc_nvmf_subsystem_get_listeners(struct spdk_jsonrpc_request *request,
}
SPDK_RPC_REGISTER("nvmf_subsystem_get_listeners", rpc_nvmf_subsystem_get_listeners,
		  SPDK_RPC_RUNTIME);

struct rpc_mdns_prr {
	char *tgt_name;
};

static const struct spdk_json_object_decoder rpc_mdns_prr_decoders[] = {
	{"tgt_name", offsetof(struct rpc_mdns_prr, tgt_name), spdk_json_decode_string, true},
};

static void
rpc_nvmf_publish_mdns_prr(struct spdk_jsonrpc_request *request,
			  const struct spdk_json_val *params)
{
#ifndef SPDK_CONFIG_AVAHI
	SPDK_ERRLOG("nvmf_publish_mdns_prr is supported when SPDK is built with the --with-avahi option.\n");
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 "nvmf_publish_mdns_prr is supported when SPDK is built with the --with-avahi option.");
	return;
#endif
	int rc;
	struct rpc_mdns_prr req = { 0 };
	struct spdk_nvmf_tgt *tgt;

	if (params) {
		if (spdk_json_decode_object(params, rpc_mdns_prr_decoders,
					    SPDK_COUNTOF(rpc_mdns_prr_decoders),
					    &req)) {
			SPDK_ERRLOG("spdk_json_decode_object failed\n");
			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
			return;
		}
	}

	tgt = spdk_nvmf_get_tgt(req.tgt_name);
	if (!tgt) {
		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
						 "Unable to find a target.");
		free(req.tgt_name);
		return;
	}

	rc = nvmf_publish_mdns_prr(tgt);
	if (rc) {
		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
		free(req.tgt_name);
		return;
	}

	spdk_jsonrpc_send_bool_response(request, true);
	free(req.tgt_name);
}
SPDK_RPC_REGISTER("nvmf_publish_mdns_prr", rpc_nvmf_publish_mdns_prr, SPDK_RPC_RUNTIME);
Loading