Commit d1fee489 authored by Vitaliy Mysak's avatar Vitaliy Mysak Committed by Darek Stojaczyk
Browse files

OCF: add OCF module

Add OCF module based on OCF meta-library
Open CAS Framwework (OCF) is high performance block storage
  caching meta-library
It is open-source, published at https://github.com/Open-CAS/ocf



With this patch OCF-enabled device is represented in SPDK
  as virtual bdev having core and caching devices as its base devices

This patch includes implementation of:
  * OCF top adapter          (vbdev_ocf.c)
  * OCF bottom adapter       (dobj.c, data.c)
  * Adaptation layer for OCF (env/)
  * OCF context abstractions (ctx.c)

Adaptation layer and context abstractions are not dependent on SPDK bdev

OCF bdev supports reads and writes, configured at startup
Other features will be added with separate patches

Change-Id: Ic2dcab378c8238d16f1e4b64d4374bdf257565bc
Signed-off-by: default avatarVitaliy Mysak <vitaliy.mysak@intel.com>
Reviewed-on: https://review.gerrithub.io/c/435708


Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent ec415110
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -124,3 +124,8 @@ CONFIG_FTL=n

# Build Intel IPSEC_MB library
CONFIG_IPSEC_MB=n

# Enable OCF module
CONFIG_OCF=n
CONFIG_OCF_PATH=
CONFIG_CUSTOMOCF=n
+44 −0
Original line number Diff line number Diff line
@@ -62,6 +62,10 @@ function usage()
	echo "                           No path required."
	echo " vtune                     Required to profile I/O under Intel VTune Amplifier XE."
	echo "                           example: /opt/intel/vtune_amplifier_xe_version"
	echo " ocf                       Required to build OCF module."
	echo "                           If argument is directory, interpret it as root of OCF repo"
	echo "                           If argument is file, interpret it as compiled OCF lib"
	echo "                           example: /usr/src/ocf/"
	echo ""
	echo "Environment variables:"
	echo ""
@@ -269,6 +273,14 @@ for i in "$@"; do
		--without-ftl)
			CONFIG[FTL]=n
			;;
		--with-ocf=*)
			CONFIG[OCF]=y
			CONFIG[OCF_PATH]=$(readlink -f ${i#*=})
			;;
		--without-ocf)
			CONFIG[OCF]=n
			CONFIG[OCF_PATH]=
			;;
		--)
			break
			;;
@@ -390,6 +402,38 @@ if [[ "${CONFIG[REDUCE]}" = "y" ]]; then
	fi
fi

if [[ "${CONFIG[OCF]}" = "y" ]]; then
	if [ -z "${CONFIG[OCF_PATH]}" ]; then
		echo "When OCF module is enabled, you must specify"
		echo "the OCF directory or path to OCF library using --with-ocf=path"
		exit 1
	fi

	# If OCF_PATH is a file, assume it is a library and use it to compile with
	if [ -f ${CONFIG[OCF_PATH]} ]; then
		CONFIG[CUSTOMOCF]=y
	else
		CONFIG[CUSTOMOCF]=n
	fi

	# If OCF_PATH is not a library, we need to do sources export procedure using OCF Makefile
	if [[ ${CONFIG[CUSTOMOCF]} = "n" ]]; then
		echo "configuring OCF..."
		rootdir=$(readlink -f $(dirname $0))

		if pushd "${CONFIG[OCF_PATH]}" > /dev/null && \
		   make inc O="$rootdir/lib/bdev/ocf/env/" && \
		   make src O="$rootdir/lib/bdev/ocf/env/" CMD=cp 1>/dev/null && \
		   popd > /dev/null
		then
			echo "done configuring OCF"
		else
			echo "Could not configure OCF"
			exit 1
		fi
	fi
fi

# We are now ready to generate final configuration. But first do sanity
# check to see if all keys in CONFIG array have its reflection in CONFIG file.
if [ $(egrep -c "^\s*CONFIG_[[:alnum:]_]+=" CONFIG) -ne ${#CONFIG[@]} ]; then
+6 −0
Original line number Diff line number Diff line
@@ -48,6 +48,12 @@ ifeq ($(CONFIG_CRYPTO),y)
DIRS-y += crypto
endif

ifeq ($(CONFIG_OCF), y)
DIRS-y += ocf
DIRS-y += ocf/env
DEPDIRS-ocf := ocf/env
endif

ifeq ($(OS),Linux)
DIRS-y += aio
DIRS-$(CONFIG_ISCSI_INITIATOR) += iscsi

lib/bdev/ocf/Makefile

0 → 100644
+47 −0
Original line number Diff line number Diff line
#
#  BSD LICENSE
#
#  Copyright (c) Intel Corporation.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in
#      the documentation and/or other materials provided with the
#      distribution.
#    * Neither the name of Intel Corporation nor the names of its
#      contributors may be used to endorse or promote products derived
#      from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../..)

include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

CFLAGS += $(ENV_CFLAGS) -I$(CURDIR)/env -I$(CURDIR)/env/include
C_SRCS = $(shell ls *.c)

LIBNAME := bdev_ocf

include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk

SPDK_DEP_LIBS = $(call spdk_lib_list_to_static_libs,ocfenv)

$(LIB) : $(SPDK_DEP_LIBS)

lib/bdev/ocf/ctx.c

0 → 100644
+413 −0
Original line number Diff line number Diff line
/*-
 *   BSD LICENSE
 *
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <ocf/ocf.h>
#include <execinfo.h>

#include "spdk/env.h"
#include "spdk_internal/log.h"

#include "ctx.h"
#include "ocf_env.h"
#include "data.h"

ocf_ctx_t vbdev_ocf_ctx;

static ctx_data_t *
vbdev_ocf_ctx_data_alloc(uint32_t pages)
{
	struct bdev_ocf_data *data;
	void *buf;
	uint32_t sz;

	data = vbdev_ocf_data_alloc(1);

	sz = pages * PAGE_SIZE;
	buf = spdk_dma_malloc(sz, PAGE_SIZE, NULL);
	if (buf == NULL) {
		return NULL;
	}

	vbdev_ocf_iovs_add(data, buf, sz);

	data->size = sz;

	return data;
}

static void
vbdev_ocf_ctx_data_free(ctx_data_t *ctx_data)
{
	struct bdev_ocf_data *data = ctx_data;
	int i;

	if (!data) {
		return;
	}

	for (i = 0; i < data->iovcnt; i++) {
		spdk_dma_free(data->iovs[i].iov_base);
	}

	vbdev_ocf_data_free(data);
}

static int
vbdev_ocf_ctx_data_mlock(ctx_data_t *ctx_data)
{
	/* TODO [mlock]: add mlock option */
	return 0;
}

static void
vbdev_ocf_ctx_data_munlock(ctx_data_t *ctx_data)
{
	/* TODO [mlock]: add mlock option */
}

static size_t
iovec_flatten(struct iovec *iov, size_t iovcnt, void *buf, size_t size, size_t offset)
{
	size_t i, len, done = 0;

	for (i = 0; i < iovcnt; i++) {
		if (offset >= iov[i].iov_len) {
			offset -= iov[i].iov_len;
			continue;
		}

		if (iov[i].iov_base == NULL) {
			continue;
		}

		if (done >= size) {
			break;
		}

		len = MIN(size - done, iov[i].iov_len - offset);
		memcpy(buf, iov[i].iov_base + offset, len);
		buf += len;
		done += len;
		offset = 0;
	}

	return done;
}

static uint32_t
vbdev_ocf_ctx_data_rd(void *dst, ctx_data_t *src, uint32_t size)
{
	struct bdev_ocf_data *s = src;
	uint32_t size_local;

	size_local = iovec_flatten(s->iovs, s->iovcnt, dst, size, s->seek);
	s->seek += size_local;

	return size_local;
}

static size_t
buf_to_iovec(const void *buf, size_t size, struct iovec *iov, size_t iovcnt, size_t offset)
{
	size_t i, len, done = 0;

	for (i = 0; i < iovcnt; i++) {
		if (offset >= iov[i].iov_len) {
			offset -= iov[i].iov_len;
			continue;
		}

		if (iov[i].iov_base == NULL) {
			continue;
		}

		if (done >= size) {
			break;
		}

		len = MIN(size - done, iov[i].iov_len - offset);
		memcpy(iov[i].iov_base + offset, buf, len);
		buf += len;
		done += len;
		offset = 0;
	}

	return done;
}

static uint32_t
vbdev_ocf_ctx_data_wr(ctx_data_t *dst, const void *src, uint32_t size)
{
	struct bdev_ocf_data *d = dst;
	uint32_t size_local;

	size_local = buf_to_iovec(src, size, d->iovs, d->iovcnt, d->seek);
	d->seek += size_local;

	return size_local;
}

static size_t
iovset(struct iovec *iov, size_t iovcnt, int byte, size_t size, size_t offset)
{
	size_t i, len, done = 0;

	for (i = 0; i < iovcnt; i++) {
		if (offset >= iov[i].iov_len) {
			offset -= iov[i].iov_len;
			continue;
		}

		if (iov[i].iov_base == NULL) {
			continue;
		}

		if (done >= size) {
			break;
		}

		len = MIN(size - done, iov[i].iov_len - offset);
		memset(iov[i].iov_base + offset, byte, len);
		done += len;
		offset = 0;
	}

	return done;
}

static uint32_t
vbdev_ocf_ctx_data_zero(ctx_data_t *dst, uint32_t size)
{
	struct bdev_ocf_data *d = dst;
	uint32_t size_local;

	size_local = iovset(d->iovs, d->iovcnt, 0, size, d->seek);
	d->seek += size_local;

	return size_local;
}

static uint32_t
vbdev_ocf_ctx_data_seek(ctx_data_t *dst, ctx_data_seek_t seek, uint32_t offset)
{
	struct bdev_ocf_data *d = dst;
	uint32_t off = 0;

	switch (seek) {
	case ctx_data_seek_begin:
		off = MIN(off, d->size);
		d->seek = off;
		break;
	case ctx_data_seek_current:
		off = MIN(off, d->size - d->seek);
		d->seek += off;
		break;
	}

	return off;
}

static uint64_t
vbdev_ocf_ctx_data_cpy(ctx_data_t *dst, ctx_data_t *src, uint64_t to,
		       uint64_t from, uint64_t bytes)
{
	struct bdev_ocf_data *s = src;
	struct bdev_ocf_data *d = dst;
	uint32_t it_iov = 0;
	uint32_t it_off = 0;
	uint32_t n, sz;

	bytes = MIN(bytes, s->size - from);
	bytes = MIN(bytes, d->size - to);
	sz = bytes;

	while (from || bytes) {
		if (s->iovs[it_iov].iov_len == it_off) {
			it_iov++;
			it_off = 0;
			continue;
		}

		if (from) {
			n = MIN(from, s->iovs[it_iov].iov_len);
			from -= n;
		} else {
			n = MIN(bytes, s->iovs[it_iov].iov_len);
			buf_to_iovec(s->iovs[it_iov].iov_base + it_off, n, d->iovs, d->iovcnt, to);
			bytes -= n;
			to += n;
		}

		it_off += n;
	}

	return sz;
}

static void
vbdev_ocf_ctx_data_secure_erase(ctx_data_t *ctx_data)
{
	struct bdev_ocf_data *data = ctx_data;
	struct iovec *iovs = data->iovs;
	int i;

	for (i = 0; i < data->iovcnt; i++) {
		if (env_memset(iovs[i].iov_base, iovs[i].iov_len, 0)) {
			assert(false);
		}
	}
}

/* OCF queue initialization procedure
 * Called during ocf_cache_start */
static int
vbdev_ocf_ctx_queue_init(ocf_queue_t q)
{
	return 0;
}

/* Called during ocf_submit_io, ocf_purge*
 * and any other requests that need to submit io */
static void
vbdev_ocf_ctx_queue_kick(ocf_queue_t q)
{
}

/* OCF queue deinitialization
 * Called at ocf_cache_stop */
static void
vbdev_ocf_ctx_queue_stop(ocf_queue_t q)
{
}

static int
vbdev_ocf_ctx_cleaner_init(ocf_cleaner_t c)
{
	/* TODO [writeback]: implement with writeback mode support */
	return 0;
}

static void
vbdev_ocf_ctx_cleaner_stop(ocf_cleaner_t c)
{
	/* TODO [writeback]: implement with writeback mode support */
}

static int vbdev_ocf_dobj_updater_init(ocf_metadata_updater_t mu)
{
	/* TODO [metadata]: implement with persistent metadata support */
	return 0;
}
static void vbdev_ocf_dobj_updater_stop(ocf_metadata_updater_t mu)
{
	/* TODO [metadata]: implement with persistent metadata support */
}
static void vbdev_ocf_dobj_updater_kick(ocf_metadata_updater_t mu)
{
	/* TODO [metadata]: implement with persistent metadata support */
}

static const struct ocf_ctx_ops vbdev_ocf_ctx_ops = {
	.name = "OCF SPDK",

	.data_alloc = vbdev_ocf_ctx_data_alloc,
	.data_free = vbdev_ocf_ctx_data_free,
	.data_mlock = vbdev_ocf_ctx_data_mlock,
	.data_munlock = vbdev_ocf_ctx_data_munlock,
	.data_rd = vbdev_ocf_ctx_data_rd,
	.data_wr = vbdev_ocf_ctx_data_wr,
	.data_zero = vbdev_ocf_ctx_data_zero,
	.data_seek = vbdev_ocf_ctx_data_seek,
	.data_cpy = vbdev_ocf_ctx_data_cpy,
	.data_secure_erase = vbdev_ocf_ctx_data_secure_erase,

	.queue_init = vbdev_ocf_ctx_queue_init,
	.queue_kick = vbdev_ocf_ctx_queue_kick,
	.queue_stop = vbdev_ocf_ctx_queue_stop,

	.cleaner_init = vbdev_ocf_ctx_cleaner_init,
	.cleaner_stop = vbdev_ocf_ctx_cleaner_stop,

	.metadata_updater_init = vbdev_ocf_dobj_updater_init,
	.metadata_updater_stop = vbdev_ocf_dobj_updater_stop,
	.metadata_updater_kick = vbdev_ocf_dobj_updater_kick,
};

/* This function is main way by which OCF communicates with user
 * We don't want to use SPDK_LOG here because debugging information that is
 * associated with every print message is not helpful in callback that only prints info
 * while the real source is somewhere in OCF code */
static int
vbdev_ocf_ctx_log_printf(const struct ocf_logger *logger,
			 ocf_logger_lvl_t lvl, const char *fmt, va_list args)
{
	FILE *lfile = stdout;

	if (lvl > log_info) {
		return 0;
	}

	if (lvl <= log_warn) {
		lfile = stderr;
	}

	return vfprintf(lfile, fmt, args);
}

static const struct ocf_logger logger = {
	.printf = vbdev_ocf_ctx_log_printf,
	.dump_stack = NULL,
};

int
vbdev_ocf_ctx_init(void)
{
	int ret;

	ret = ocf_ctx_init(&vbdev_ocf_ctx, &vbdev_ocf_ctx_ops);
	if (ret < 0) {
		return ret;
	}

	ocf_ctx_set_logger(vbdev_ocf_ctx, &logger);

	return 0;
}

void
vbdev_ocf_ctx_cleanup(void)
{
	ocf_ctx_exit(vbdev_ocf_ctx);
	vbdev_ocf_ctx = NULL;
}

SPDK_LOG_REGISTER_COMPONENT("ocf_ocfctx", SPDK_LOG_OCFCTX)
Loading