Commit 35366140 authored by Vitaliy Mysak's avatar Vitaliy Mysak Committed by Jim Harris
Browse files

OCF: add RPC



Add rpc methods for vbdev_ocf:
  * construct_ocf_bdev
  * delete_ocf_bdev

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


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
parent a4c344c1
Loading
Loading
Loading
Loading
+82 −0
Original line number Diff line number Diff line
@@ -790,6 +790,88 @@ Example response:
}
~~~

## construct_ocf_bdev {#rpc_construct_ocf_bdev}

Construct new OCF bdev.
Command accepts cache mode that is going to be used.
Currently, we support Write-Through and Pass-Through OCF cache modes.

### Parameters

Name                    | Optional | Type        | Description
----------------------- | -------- | ----------- | -----------
name                    | Required | string      | Bdev name to use
mode                    | Required | string      | OCF cache mode ('wt' or 'pt')
cache_bdev_name         | Required | string      | Name of underlying cache bdev
core_bdev_name          | Required | string      | Name of underlying core bdev

### Result

Name of newly created bdev.

### Example

Example request:

~~~
{
  "params": {
    "name": "ocf0",
    "mode": "wt",
    "cache_bdev_name": "Nvme0n1"
    "core_bdev_name": "aio0"
  },
  "jsonrpc": "2.0",
  "method": "construct_ocf_bdev",
  "id": 1
}
~~~

Example response:

~~~
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "ocf0"
}
~~~

## delete_ocf_bdev {#rpc_delete_ocf_bdev}

Delete the OCF bdev

### Parameters

Name                    | Optional | Type        | Description
----------------------- | -------- | ----------- | -----------
name                    | Required | string      | Bdev name

### Example

Example request:

~~~
{
  "params": {
    "name": "ocf0"
  },
  "jsonrpc": "2.0",
  "method": "delete_ocf_bdev",
  "id": 1
}
~~~

Example response:

~~~
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": true
}
~~~

## construct_malloc_bdev {#rpc_construct_malloc_bdev}

Construct @ref bdev_config_malloc
+160 −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 "vbdev_ocf.h"
#include "spdk/log.h"
#include "spdk/rpc.h"
#include "spdk/string.h"

/* Structure to hold the parameters for this RPC method. */
struct rpc_construct_ocf_bdev {
	char *name;             /* master vbdev */
	char *mode;             /* OCF mode (choose one) */
	char *cache_bdev_name;  /* sub bdev */
	char *core_bdev_name;   /* sub bdev */
};

static void
free_rpc_construct_ocf_bdev(struct rpc_construct_ocf_bdev *r)
{
	free(r->name);
	free(r->core_bdev_name);
	free(r->cache_bdev_name);
	free(r->mode);
}

/* Structure to decode the input parameters for this RPC method. */
static const struct spdk_json_object_decoder rpc_construct_ocf_bdev_decoders[] = {
	{"name", offsetof(struct rpc_construct_ocf_bdev, name), spdk_json_decode_string},
	{"mode", offsetof(struct rpc_construct_ocf_bdev, mode), spdk_json_decode_string},
	{"cache_bdev_name", offsetof(struct rpc_construct_ocf_bdev, cache_bdev_name), spdk_json_decode_string},
	{"core_bdev_name", offsetof(struct rpc_construct_ocf_bdev, core_bdev_name), spdk_json_decode_string},
};

static void
spdk_rpc_construct_ocf_bdev(struct spdk_jsonrpc_request *request,
			    const struct spdk_json_val *params)
{
	int ret = 0;
	struct rpc_construct_ocf_bdev req = {NULL};
	struct spdk_json_write_ctx *w;

	ret = spdk_json_decode_object(params, rpc_construct_ocf_bdev_decoders,
				      SPDK_COUNTOF(rpc_construct_ocf_bdev_decoders),
				      &req);
	if (ret) {
		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
						 "Invalid parameters");
		goto end;
	}

	ret = vbdev_ocf_construct(req.name, req.mode, req.cache_bdev_name, req.core_bdev_name);
	if (ret) {
		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
						     "Could not create OCF vbdev: %s",
						     spdk_strerror(-ret));
		goto end;
	}

	w = spdk_jsonrpc_begin_result(request);
	if (w) {
		spdk_json_write_string(w, req.name);
		spdk_jsonrpc_end_result(request, w);
	}

end:
	free_rpc_construct_ocf_bdev(&req);
}
SPDK_RPC_REGISTER("construct_ocf_bdev", spdk_rpc_construct_ocf_bdev, SPDK_RPC_RUNTIME)

/* Structure to hold the parameters for this RPC method. */
struct rpc_delete_ocf_bdev {
	char *name;             /* master vbdev name */
};

static void
free_rpc_delete_ocf_bdev(struct rpc_delete_ocf_bdev *r)
{
	free(r->name);
}

/* Structure to decode the input parameters for this RPC method. */
static const struct spdk_json_object_decoder rpc_delete_ocf_bdev_decoders[] = {
	{"name", offsetof(struct rpc_delete_ocf_bdev, name), spdk_json_decode_string},
};

static void
spdk_rpc_delete_ocf_bdev(struct spdk_jsonrpc_request *request,
			 const struct spdk_json_val *params)
{
	struct rpc_delete_ocf_bdev req = {NULL};
	struct spdk_json_write_ctx *w;
	struct vbdev_ocf *vbdev;
	int status;

	status = spdk_json_decode_object(params, rpc_delete_ocf_bdev_decoders,
					 SPDK_COUNTOF(rpc_delete_ocf_bdev_decoders),
					 &req);
	if (status) {
		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
						 "Invalid parameters");
		goto end;
	}

	vbdev = vbdev_ocf_get_by_name(req.name);
	if (vbdev == NULL) {
		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
						 spdk_strerror(ENODEV));
		goto end;
	}

	status = vbdev_ocf_delete(vbdev);
	if (status) {
		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
						     "Could not delete OCF vbdev: %s",
						     spdk_strerror(-status));
		goto end;
	}

	w = spdk_jsonrpc_begin_result(request);
	if (w == NULL) {
		goto end;
	}

	spdk_json_write_bool(w, true);
	spdk_jsonrpc_end_result(request, w);

end:
	free_rpc_delete_ocf_bdev(&req);
}
SPDK_RPC_REGISTER("delete_ocf_bdev", spdk_rpc_delete_ocf_bdev, SPDK_RPC_RUNTIME)
+23 −0
Original line number Diff line number Diff line
@@ -144,6 +144,29 @@ if __name__ == "__main__":
    p.add_argument('name', help='crypto bdev name')
    p.set_defaults(func=delete_crypto_bdev)

    def construct_ocf_bdev(args):
        print(rpc.bdev.construct_ocf_bdev(args.client,
                                          name=args.name,
                                          mode=args.mode,
                                          cache_bdev_name=args.cache_bdev_name,
                                          core_bdev_name=args.core_bdev_name))
    p = subparsers.add_parser('construct_ocf_bdev',
                              help='Add an OCF block device')
    p.add_argument('name', help='Name of resulting OCF bdev')
    p.add_argument('mode', help='OCF cache mode', choices=['wt', 'pt'])
    p.add_argument('cache_bdev_name', help='Name of underlying cache bdev')
    p.add_argument('core_bdev_name', help='Name of unerlying core bdev')
    p.set_defaults(func=construct_ocf_bdev)

    def delete_ocf_bdev(args):
        rpc.bdev.delete_ocf_bdev(args.client,
                                 name=args.name)

    p = subparsers.add_parser('delete_ocf_bdev',
                              help='Delete an OCF block device')
    p.add_argument('name', help='Name of OCF bdev')
    p.set_defaults(func=delete_ocf_bdev)

    def construct_malloc_bdev(args):
        num_blocks = (args.total_size * 1024 * 1024) // args.block_size
        print(rpc.bdev.construct_malloc_bdev(args.client,
+29 −0
Original line number Diff line number Diff line
@@ -42,6 +42,35 @@ def delete_crypto_bdev(client, name):
    return client.call('delete_crypto_bdev', params)


def construct_ocf_bdev(client, name, mode, cache_bdev_name, core_bdev_name):
    """Add an OCF block device

    Args:
        name: name of constructed OCF bdev
        mode: OCF cache mode: {'wt', 'pt'}
        cache_bdev_name: name of underlying cache bdev
        core_bdev_name: name of underlying core bdev

    Returns:
        Name of created block device
    """
    params = {'name': name, 'mode': mode, 'cache_bdev_name': cache_bdev_name, 'core_bdev_name': core_bdev_name}

    return client.call('construct_ocf_bdev', params)


def delete_ocf_bdev(client, name):
    """Delete an OCF device

    Args:
        name: name of OCF bdev

    """
    params = {'name': name}

    return client.call('delete_ocf_bdev', params)


def construct_malloc_bdev(client, num_blocks, block_size, name=None, uuid=None):
    """Construct a malloc block device.