xref: /spdk/module/bdev/delay/vbdev_delay_rpc.c (revision e85f1f11ec28647eaae4ad74ab118253d0664eac)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2a6dbe372Spaul luse  *   Copyright (C) 2019 Intel Corporation.
307fe6a43SSeth Howell  *   All rights reserved.
407fe6a43SSeth Howell  */
507fe6a43SSeth Howell 
607fe6a43SSeth Howell #include "vbdev_delay.h"
707fe6a43SSeth Howell #include "spdk/rpc.h"
807fe6a43SSeth Howell #include "spdk/util.h"
907fe6a43SSeth Howell #include "spdk/string.h"
104e8e97c8STomasz Zawadzki #include "spdk/log.h"
11aa0dad57SSeth Howell #include "spdk_internal/assert.h"
1207fe6a43SSeth Howell 
1307fe6a43SSeth Howell struct rpc_update_latency {
1407fe6a43SSeth Howell 	char *delay_bdev_name;
1507fe6a43SSeth Howell 	char *latency_type;
1607fe6a43SSeth Howell 	uint64_t latency_us;
1707fe6a43SSeth Howell };
1807fe6a43SSeth Howell 
1907fe6a43SSeth Howell static const struct spdk_json_object_decoder rpc_update_latency_decoders[] = {
2007fe6a43SSeth Howell 	{"delay_bdev_name", offsetof(struct rpc_update_latency, delay_bdev_name), spdk_json_decode_string},
2107fe6a43SSeth Howell 	{"latency_type", offsetof(struct rpc_update_latency, latency_type), spdk_json_decode_string},
2207fe6a43SSeth Howell 	{"latency_us", offsetof(struct rpc_update_latency, latency_us), spdk_json_decode_uint64}
2307fe6a43SSeth Howell };
2407fe6a43SSeth Howell 
2507fe6a43SSeth Howell static void
free_rpc_update_latency(struct rpc_update_latency * req)2607fe6a43SSeth Howell free_rpc_update_latency(struct rpc_update_latency *req)
2707fe6a43SSeth Howell {
2807fe6a43SSeth Howell 	free(req->delay_bdev_name);
2907fe6a43SSeth Howell 	free(req->latency_type);
3007fe6a43SSeth Howell }
3107fe6a43SSeth Howell 
3207fe6a43SSeth Howell static void
rpc_bdev_delay_update_latency(struct spdk_jsonrpc_request * request,const struct spdk_json_val * params)33684cf603SSeth Howell rpc_bdev_delay_update_latency(struct spdk_jsonrpc_request *request,
3407fe6a43SSeth Howell 			      const struct spdk_json_val *params)
3507fe6a43SSeth Howell {
3607fe6a43SSeth Howell 	struct rpc_update_latency req = {NULL};
3707fe6a43SSeth Howell 	enum delay_io_type latency_type;
3807fe6a43SSeth Howell 	int rc = 0;
3907fe6a43SSeth Howell 
4007fe6a43SSeth Howell 	if (spdk_json_decode_object(params, rpc_update_latency_decoders,
4107fe6a43SSeth Howell 				    SPDK_COUNTOF(rpc_update_latency_decoders),
4207fe6a43SSeth Howell 				    &req)) {
432172c432STomasz Zawadzki 		SPDK_DEBUGLOG(vbdev_delay, "spdk_json_decode_object failed\n");
4407fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
4507fe6a43SSeth Howell 						 "spdk_json_decode_object failed");
4607fe6a43SSeth Howell 		goto cleanup;
4707fe6a43SSeth Howell 	}
4807fe6a43SSeth Howell 
4907fe6a43SSeth Howell 	if (!strncmp(req.latency_type, "avg_read", 9)) {
5007fe6a43SSeth Howell 		latency_type = DELAY_AVG_READ;
5107fe6a43SSeth Howell 	} else if (!strncmp(req.latency_type, "p99_read", 9)) {
5207fe6a43SSeth Howell 		latency_type = DELAY_P99_READ;
5307fe6a43SSeth Howell 	} else if (!strncmp(req.latency_type, "avg_write", 10)) {
5407fe6a43SSeth Howell 		latency_type = DELAY_AVG_WRITE;
5507fe6a43SSeth Howell 	} else if (!strncmp(req.latency_type, "p99_write", 10)) {
5607fe6a43SSeth Howell 		latency_type = DELAY_P99_WRITE;
5707fe6a43SSeth Howell 	} else {
5807fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
5907fe6a43SSeth Howell 						 "Please specify a valid latency type.");
6007fe6a43SSeth Howell 		goto cleanup;
6107fe6a43SSeth Howell 	}
6207fe6a43SSeth Howell 
6307fe6a43SSeth Howell 	rc = vbdev_delay_update_latency_value(req.delay_bdev_name, req.latency_us, latency_type);
6407fe6a43SSeth Howell 
6507fe6a43SSeth Howell 	if (rc == -ENODEV) {
6607fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
6707fe6a43SSeth Howell 						 "The requested bdev does not exist.");
6807fe6a43SSeth Howell 		goto cleanup;
6907fe6a43SSeth Howell 	} else if (rc == -EINVAL) {
7007fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST,
7107fe6a43SSeth Howell 						 "The requested bdev is not a delay bdev.");
7207fe6a43SSeth Howell 		goto cleanup;
7307fe6a43SSeth Howell 	} else if (rc) {
74aa0dad57SSeth Howell 		SPDK_UNREACHABLE();
7507fe6a43SSeth Howell 	}
7607fe6a43SSeth Howell 
77d73077b8Syidong0635 	spdk_jsonrpc_send_bool_response(request, true);
7807fe6a43SSeth Howell 
7907fe6a43SSeth Howell cleanup:
8007fe6a43SSeth Howell 	free_rpc_update_latency(&req);
8107fe6a43SSeth Howell }
82684cf603SSeth Howell SPDK_RPC_REGISTER("bdev_delay_update_latency", rpc_bdev_delay_update_latency, SPDK_RPC_RUNTIME)
8307fe6a43SSeth Howell 
8407fe6a43SSeth Howell struct rpc_construct_delay {
8507fe6a43SSeth Howell 	char *base_bdev_name;
8607fe6a43SSeth Howell 	char *name;
87*e85f1f11SKonrad Sztyber 	struct spdk_uuid uuid;
8807fe6a43SSeth Howell 	uint64_t avg_read_latency;
8907fe6a43SSeth Howell 	uint64_t p99_read_latency;
9007fe6a43SSeth Howell 	uint64_t avg_write_latency;
9107fe6a43SSeth Howell 	uint64_t p99_write_latency;
9207fe6a43SSeth Howell };
9307fe6a43SSeth Howell 
9407fe6a43SSeth Howell static void
free_rpc_construct_delay(struct rpc_construct_delay * r)9507fe6a43SSeth Howell free_rpc_construct_delay(struct rpc_construct_delay *r)
9607fe6a43SSeth Howell {
9707fe6a43SSeth Howell 	free(r->base_bdev_name);
9807fe6a43SSeth Howell 	free(r->name);
9907fe6a43SSeth Howell }
10007fe6a43SSeth Howell 
10107fe6a43SSeth Howell static const struct spdk_json_object_decoder rpc_construct_delay_decoders[] = {
10207fe6a43SSeth Howell 	{"base_bdev_name", offsetof(struct rpc_construct_delay, base_bdev_name), spdk_json_decode_string},
10307fe6a43SSeth Howell 	{"name", offsetof(struct rpc_construct_delay, name), spdk_json_decode_string},
104*e85f1f11SKonrad Sztyber 	{"uuid", offsetof(struct rpc_construct_delay, uuid), spdk_json_decode_uuid, true},
10507fe6a43SSeth Howell 	{"avg_read_latency", offsetof(struct rpc_construct_delay, avg_read_latency), spdk_json_decode_uint64},
10607fe6a43SSeth Howell 	{"p99_read_latency", offsetof(struct rpc_construct_delay, p99_read_latency), spdk_json_decode_uint64},
10707fe6a43SSeth Howell 	{"avg_write_latency", offsetof(struct rpc_construct_delay, avg_write_latency), spdk_json_decode_uint64},
10807fe6a43SSeth Howell 	{"p99_write_latency", offsetof(struct rpc_construct_delay, p99_write_latency), spdk_json_decode_uint64},
10907fe6a43SSeth Howell };
11007fe6a43SSeth Howell 
11107fe6a43SSeth Howell static void
rpc_bdev_delay_create(struct spdk_jsonrpc_request * request,const struct spdk_json_val * params)112684cf603SSeth Howell rpc_bdev_delay_create(struct spdk_jsonrpc_request *request,
11307fe6a43SSeth Howell 		      const struct spdk_json_val *params)
11407fe6a43SSeth Howell {
11507fe6a43SSeth Howell 	struct rpc_construct_delay req = {NULL};
11607fe6a43SSeth Howell 	struct spdk_json_write_ctx *w;
11707fe6a43SSeth Howell 	int rc;
11807fe6a43SSeth Howell 
11907fe6a43SSeth Howell 	if (spdk_json_decode_object(params, rpc_construct_delay_decoders,
12007fe6a43SSeth Howell 				    SPDK_COUNTOF(rpc_construct_delay_decoders),
12107fe6a43SSeth Howell 				    &req)) {
1222172c432STomasz Zawadzki 		SPDK_DEBUGLOG(vbdev_delay, "spdk_json_decode_object failed\n");
12307fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
12407fe6a43SSeth Howell 						 "spdk_json_decode_object failed");
12507fe6a43SSeth Howell 		goto cleanup;
12607fe6a43SSeth Howell 	}
12707fe6a43SSeth Howell 
128*e85f1f11SKonrad Sztyber 	rc = create_delay_disk(req.base_bdev_name, req.name, &req.uuid, req.avg_read_latency,
1292f08dc7fSTomasz Zawadzki 			       req.p99_read_latency,
13007fe6a43SSeth Howell 			       req.avg_write_latency, req.p99_write_latency);
13107fe6a43SSeth Howell 	if (rc != 0) {
13207fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
13307fe6a43SSeth Howell 		goto cleanup;
13407fe6a43SSeth Howell 	}
13507fe6a43SSeth Howell 
13607fe6a43SSeth Howell 	w = spdk_jsonrpc_begin_result(request);
13707fe6a43SSeth Howell 	spdk_json_write_string(w, req.name);
13807fe6a43SSeth Howell 	spdk_jsonrpc_end_result(request, w);
13907fe6a43SSeth Howell 
14007fe6a43SSeth Howell cleanup:
14107fe6a43SSeth Howell 	free_rpc_construct_delay(&req);
14207fe6a43SSeth Howell }
143684cf603SSeth Howell SPDK_RPC_REGISTER("bdev_delay_create", rpc_bdev_delay_create, SPDK_RPC_RUNTIME)
14407fe6a43SSeth Howell 
14507fe6a43SSeth Howell struct rpc_delete_delay {
14607fe6a43SSeth Howell 	char *name;
14707fe6a43SSeth Howell };
14807fe6a43SSeth Howell 
14907fe6a43SSeth Howell static void
free_rpc_delete_delay(struct rpc_delete_delay * req)15007fe6a43SSeth Howell free_rpc_delete_delay(struct rpc_delete_delay *req)
15107fe6a43SSeth Howell {
15207fe6a43SSeth Howell 	free(req->name);
15307fe6a43SSeth Howell }
15407fe6a43SSeth Howell 
15507fe6a43SSeth Howell static const struct spdk_json_object_decoder rpc_delete_delay_decoders[] = {
15607fe6a43SSeth Howell 	{"name", offsetof(struct rpc_delete_delay, name), spdk_json_decode_string},
15707fe6a43SSeth Howell };
15807fe6a43SSeth Howell 
15907fe6a43SSeth Howell static void
rpc_bdev_delay_delete_cb(void * cb_arg,int bdeverrno)160684cf603SSeth Howell rpc_bdev_delay_delete_cb(void *cb_arg, int bdeverrno)
16107fe6a43SSeth Howell {
16207fe6a43SSeth Howell 	struct spdk_jsonrpc_request *request = cb_arg;
16307fe6a43SSeth Howell 
164d3e394aeSShuhei Matsumoto 	if (bdeverrno == 0) {
165d3e394aeSShuhei Matsumoto 		spdk_jsonrpc_send_bool_response(request, true);
166d3e394aeSShuhei Matsumoto 	} else {
167d3e394aeSShuhei Matsumoto 		spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
168d3e394aeSShuhei Matsumoto 	}
16907fe6a43SSeth Howell }
17007fe6a43SSeth Howell 
17107fe6a43SSeth Howell static void
rpc_bdev_delay_delete(struct spdk_jsonrpc_request * request,const struct spdk_json_val * params)172684cf603SSeth Howell rpc_bdev_delay_delete(struct spdk_jsonrpc_request *request,
17307fe6a43SSeth Howell 		      const struct spdk_json_val *params)
17407fe6a43SSeth Howell {
17507fe6a43SSeth Howell 	struct rpc_delete_delay req = {NULL};
17607fe6a43SSeth Howell 
17707fe6a43SSeth Howell 	if (spdk_json_decode_object(params, rpc_delete_delay_decoders,
17807fe6a43SSeth Howell 				    SPDK_COUNTOF(rpc_delete_delay_decoders),
17907fe6a43SSeth Howell 				    &req)) {
18007fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
18107fe6a43SSeth Howell 						 "spdk_json_decode_object failed");
18207fe6a43SSeth Howell 		goto cleanup;
18307fe6a43SSeth Howell 	}
18407fe6a43SSeth Howell 
1854573e4ccSShuhei Matsumoto 	delete_delay_disk(req.name, rpc_bdev_delay_delete_cb, request);
18607fe6a43SSeth Howell 
18707fe6a43SSeth Howell cleanup:
18807fe6a43SSeth Howell 	free_rpc_delete_delay(&req);
18907fe6a43SSeth Howell }
190684cf603SSeth Howell SPDK_RPC_REGISTER("bdev_delay_delete", rpc_bdev_delay_delete, SPDK_RPC_RUNTIME)
191