xref: /spdk/module/event/subsystems/nvmf/nvmf_rpc.c (revision 407e88fd2ab020d753e33014cf759353a9901b51)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2018-2019 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "event_nvmf.h"
35 
36 #include "spdk/rpc.h"
37 #include "spdk/util.h"
38 
39 static const struct spdk_json_object_decoder nvmf_rpc_subsystem_tgt_opts_decoder[] = {
40 	{"max_subsystems", 0, spdk_json_decode_uint32, true}
41 };
42 
43 static void
44 spdk_rpc_set_nvmf_target_max_subsystems(struct spdk_jsonrpc_request *request,
45 					const struct spdk_json_val *params)
46 {
47 	struct spdk_json_write_ctx *w;
48 	uint32_t max_subsystems = 0;
49 
50 	if (g_spdk_nvmf_tgt_max_subsystems != 0) {
51 		SPDK_ERRLOG("this RPC must not be called more than once.\n");
52 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
53 						 "Must not call more than once");
54 		return;
55 	}
56 
57 	if (params != NULL) {
58 		if (spdk_json_decode_object(params, nvmf_rpc_subsystem_tgt_opts_decoder,
59 					    SPDK_COUNTOF(nvmf_rpc_subsystem_tgt_opts_decoder), &max_subsystems)) {
60 			SPDK_ERRLOG("spdk_json_decode_object() failed\n");
61 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
62 							 "Invalid parameters");
63 			return;
64 		}
65 	}
66 
67 	g_spdk_nvmf_tgt_max_subsystems = max_subsystems;
68 
69 	w = spdk_jsonrpc_begin_result(request);
70 	spdk_json_write_bool(w, true);
71 	spdk_jsonrpc_end_result(request, w);
72 }
73 SPDK_RPC_REGISTER("set_nvmf_target_max_subsystems", spdk_rpc_set_nvmf_target_max_subsystems,
74 		  SPDK_RPC_STARTUP)
75 
76 static int decode_conn_sched(const struct spdk_json_val *val, void *out)
77 {
78 	enum spdk_nvmf_connect_sched *sched = out;
79 
80 	if (spdk_json_strequal(val, "roundrobin") == true) {
81 		*sched = CONNECT_SCHED_ROUND_ROBIN;
82 	} else if (spdk_json_strequal(val, "hostip") == true) {
83 		*sched = CONNECT_SCHED_HOST_IP;
84 	} else if (spdk_json_strequal(val, "transport") == true) {
85 		*sched = CONNECT_SCHED_TRANSPORT_OPTIMAL_GROUP;
86 	} else {
87 		SPDK_ERRLOG("Invalid connection scheduling parameter\n");
88 		return -EINVAL;
89 	}
90 
91 	return 0;
92 }
93 
94 static const struct spdk_json_object_decoder nvmf_rpc_subsystem_tgt_conf_decoder[] = {
95 	{"acceptor_poll_rate", offsetof(struct spdk_nvmf_tgt_conf, acceptor_poll_rate), spdk_json_decode_uint32, true},
96 	{"conn_sched", offsetof(struct spdk_nvmf_tgt_conf, conn_sched), decode_conn_sched, true},
97 };
98 
99 static void
100 spdk_rpc_set_nvmf_target_config(struct spdk_jsonrpc_request *request,
101 				const struct spdk_json_val *params)
102 {
103 	struct spdk_nvmf_tgt_conf *conf;
104 	struct spdk_json_write_ctx *w;
105 
106 	if (g_spdk_nvmf_tgt_conf != NULL) {
107 		SPDK_ERRLOG("this RPC must not be called more than once.\n");
108 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
109 						 "Must not call more than once");
110 		return;
111 	}
112 
113 	conf = calloc(1, sizeof(*conf));
114 	if (conf == NULL) {
115 		SPDK_ERRLOG("calloc() failed for target config\n");
116 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
117 						 "Out of memory");
118 		return;
119 	}
120 
121 	conf->acceptor_poll_rate = ACCEPT_TIMEOUT_US;
122 	conf->conn_sched = DEFAULT_CONN_SCHED;
123 
124 	if (params != NULL) {
125 		if (spdk_json_decode_object(params, nvmf_rpc_subsystem_tgt_conf_decoder,
126 					    SPDK_COUNTOF(nvmf_rpc_subsystem_tgt_conf_decoder), conf)) {
127 			free(conf);
128 			SPDK_ERRLOG("spdk_json_decode_object() failed\n");
129 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
130 							 "Invalid parameters");
131 			return;
132 		}
133 	}
134 
135 	g_spdk_nvmf_tgt_conf = conf;
136 
137 	w = spdk_jsonrpc_begin_result(request);
138 	spdk_json_write_bool(w, true);
139 	spdk_jsonrpc_end_result(request, w);
140 }
141 SPDK_RPC_REGISTER("set_nvmf_target_config", spdk_rpc_set_nvmf_target_config, SPDK_RPC_STARTUP)
142