xref: /spdk/lib/event/app_rpc.c (revision 441431d22872ae4e05a1bf8b78e9aeff1eba1eb3)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 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 "spdk/stdinc.h"
35 
36 #include "spdk/event.h"
37 #include "spdk/rpc.h"
38 #include "spdk/string.h"
39 #include "spdk/util.h"
40 #include "spdk/env.h"
41 #include "spdk/thread.h"
42 
43 #include "spdk/log.h"
44 #include "spdk_internal/event.h"
45 #include "spdk_internal/thread.h"
46 
47 struct rpc_spdk_kill_instance {
48 	char *sig_name;
49 };
50 
51 static void
52 free_rpc_spdk_kill_instance(struct rpc_spdk_kill_instance *req)
53 {
54 	free(req->sig_name);
55 }
56 
57 static const struct spdk_json_object_decoder rpc_spdk_kill_instance_decoders[] = {
58 	{"sig_name", offsetof(struct rpc_spdk_kill_instance, sig_name), spdk_json_decode_string},
59 };
60 
61 static void
62 rpc_spdk_kill_instance(struct spdk_jsonrpc_request *request,
63 		       const struct spdk_json_val *params)
64 {
65 	static const struct {
66 		const char	*signal_string;
67 		int32_t		signal;
68 	} signals[] = {
69 		{"SIGINT",	SIGINT},
70 		{"SIGTERM",	SIGTERM},
71 		{"SIGQUIT",	SIGQUIT},
72 		{"SIGHUP",	SIGHUP},
73 		{"SIGKILL",	SIGKILL},
74 		{"SIGUSR1",	SIGUSR1},
75 	};
76 	size_t i, sig_count;
77 	int signal;
78 	struct rpc_spdk_kill_instance req = {};
79 
80 	if (spdk_json_decode_object(params, rpc_spdk_kill_instance_decoders,
81 				    SPDK_COUNTOF(rpc_spdk_kill_instance_decoders),
82 				    &req)) {
83 		SPDK_DEBUGLOG(app_rpc, "spdk_json_decode_object failed\n");
84 		goto invalid;
85 	}
86 
87 	sig_count = SPDK_COUNTOF(signals);
88 	signal = spdk_strtol(req.sig_name, 10);
89 	for (i = 0 ; i < sig_count; i++) {
90 		if (strcmp(req.sig_name, signals[i].signal_string) == 0 ||
91 		    signal == signals[i].signal) {
92 			break;
93 		}
94 	}
95 
96 	if (i == sig_count) {
97 		goto invalid;
98 	}
99 
100 	SPDK_DEBUGLOG(app_rpc, "sending signal %d\n", signals[i].signal);
101 	free_rpc_spdk_kill_instance(&req);
102 	kill(getpid(), signals[i].signal);
103 
104 	spdk_jsonrpc_send_bool_response(request, true);
105 	return;
106 
107 invalid:
108 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
109 	free_rpc_spdk_kill_instance(&req);
110 }
111 SPDK_RPC_REGISTER("spdk_kill_instance", rpc_spdk_kill_instance, SPDK_RPC_RUNTIME)
112 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(spdk_kill_instance, kill_instance)
113 
114 
115 struct rpc_framework_monitor_context_switch {
116 	bool enabled;
117 };
118 
119 static const struct spdk_json_object_decoder rpc_framework_monitor_context_switch_decoders[] = {
120 	{"enabled", offsetof(struct rpc_framework_monitor_context_switch, enabled), spdk_json_decode_bool},
121 };
122 
123 static void
124 rpc_framework_monitor_context_switch(struct spdk_jsonrpc_request *request,
125 				     const struct spdk_json_val *params)
126 {
127 	struct rpc_framework_monitor_context_switch req = {};
128 	struct spdk_json_write_ctx *w;
129 
130 	if (params != NULL) {
131 		if (spdk_json_decode_object(params, rpc_framework_monitor_context_switch_decoders,
132 					    SPDK_COUNTOF(rpc_framework_monitor_context_switch_decoders),
133 					    &req)) {
134 			SPDK_DEBUGLOG(app_rpc, "spdk_json_decode_object failed\n");
135 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
136 			return;
137 		}
138 
139 		spdk_framework_enable_context_switch_monitor(req.enabled);
140 	}
141 
142 	w = spdk_jsonrpc_begin_result(request);
143 	spdk_json_write_object_begin(w);
144 
145 	spdk_json_write_named_bool(w, "enabled", spdk_framework_context_switch_monitor_enabled());
146 
147 	spdk_json_write_object_end(w);
148 	spdk_jsonrpc_end_result(request, w);
149 }
150 
151 SPDK_RPC_REGISTER("framework_monitor_context_switch", rpc_framework_monitor_context_switch,
152 		  SPDK_RPC_RUNTIME)
153 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_monitor_context_switch, context_switch_monitor)
154 
155 struct rpc_get_stats_ctx {
156 	struct spdk_jsonrpc_request *request;
157 	struct spdk_json_write_ctx *w;
158 	uint64_t now;
159 };
160 
161 static void
162 rpc_thread_get_stats_done(void *arg)
163 {
164 	struct rpc_get_stats_ctx *ctx = arg;
165 
166 	spdk_json_write_array_end(ctx->w);
167 	spdk_json_write_object_end(ctx->w);
168 	spdk_jsonrpc_end_result(ctx->request, ctx->w);
169 
170 	free(ctx);
171 }
172 
173 static void
174 rpc_thread_get_stats_for_each(struct spdk_jsonrpc_request *request, spdk_msg_fn fn)
175 {
176 	struct rpc_get_stats_ctx *ctx;
177 
178 	ctx = calloc(1, sizeof(*ctx));
179 	if (!ctx) {
180 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
181 						 "Memory allocation error");
182 		return;
183 	}
184 	ctx->request = request;
185 
186 	ctx->w = spdk_jsonrpc_begin_result(ctx->request);
187 	spdk_json_write_object_begin(ctx->w);
188 	spdk_json_write_named_uint64(ctx->w, "tick_rate", spdk_get_ticks_hz());
189 	spdk_json_write_named_array_begin(ctx->w, "threads");
190 
191 	spdk_for_each_thread(fn, ctx, rpc_thread_get_stats_done);
192 }
193 
194 static void
195 _rpc_thread_get_stats(void *arg)
196 {
197 	struct rpc_get_stats_ctx *ctx = arg;
198 	struct spdk_thread *thread = spdk_get_thread();
199 	struct spdk_poller *poller;
200 	struct spdk_thread_stats stats;
201 	uint64_t active_pollers_count = 0;
202 	uint64_t timed_pollers_count = 0;
203 	uint64_t paused_pollers_count = 0;
204 
205 	for (poller = spdk_thread_get_first_active_poller(thread); poller != NULL;
206 	     poller = spdk_thread_get_next_active_poller(poller)) {
207 		active_pollers_count++;
208 	}
209 
210 	for (poller = spdk_thread_get_first_timed_poller(thread); poller != NULL;
211 	     poller = spdk_thread_get_next_timed_poller(poller)) {
212 		timed_pollers_count++;
213 	}
214 
215 	for (poller = spdk_thread_get_first_paused_poller(thread); poller != NULL;
216 	     poller = spdk_thread_get_next_paused_poller(poller)) {
217 		paused_pollers_count++;
218 	}
219 
220 	if (0 == spdk_thread_get_stats(&stats)) {
221 		spdk_json_write_object_begin(ctx->w);
222 		spdk_json_write_named_string(ctx->w, "name", spdk_thread_get_name(thread));
223 		spdk_json_write_named_uint64(ctx->w, "id", spdk_thread_get_id(thread));
224 		spdk_json_write_named_string(ctx->w, "cpumask",
225 					     spdk_cpuset_fmt(spdk_thread_get_cpumask(thread)));
226 		spdk_json_write_named_uint64(ctx->w, "busy", stats.busy_tsc);
227 		spdk_json_write_named_uint64(ctx->w, "idle", stats.idle_tsc);
228 		spdk_json_write_named_uint64(ctx->w, "active_pollers_count", active_pollers_count);
229 		spdk_json_write_named_uint64(ctx->w, "timed_pollers_count", timed_pollers_count);
230 		spdk_json_write_named_uint64(ctx->w, "paused_pollers_count", paused_pollers_count);
231 		spdk_json_write_object_end(ctx->w);
232 	}
233 }
234 
235 static void
236 rpc_thread_get_stats(struct spdk_jsonrpc_request *request,
237 		     const struct spdk_json_val *params)
238 {
239 	if (params) {
240 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
241 						 "'thread_get_stats' requires no arguments");
242 		return;
243 	}
244 
245 	rpc_thread_get_stats_for_each(request, _rpc_thread_get_stats);
246 }
247 
248 SPDK_RPC_REGISTER("thread_get_stats", rpc_thread_get_stats, SPDK_RPC_RUNTIME)
249 
250 static void
251 rpc_get_poller(struct spdk_poller *poller, struct spdk_json_write_ctx *w)
252 {
253 	struct spdk_poller_stats stats;
254 	uint64_t period_ticks;
255 
256 	period_ticks = spdk_poller_get_period_ticks(poller);
257 	spdk_poller_get_stats(poller, &stats);
258 
259 	spdk_json_write_object_begin(w);
260 	spdk_json_write_named_string(w, "name", spdk_poller_get_name(poller));
261 	spdk_json_write_named_string(w, "state", spdk_poller_get_state_str(poller));
262 	spdk_json_write_named_uint64(w, "run_count", stats.run_count);
263 	spdk_json_write_named_uint64(w, "busy_count", stats.busy_count);
264 	if (period_ticks) {
265 		spdk_json_write_named_uint64(w, "period_ticks", period_ticks);
266 	}
267 	spdk_json_write_object_end(w);
268 }
269 
270 static void
271 _rpc_thread_get_pollers(void *arg)
272 {
273 	struct rpc_get_stats_ctx *ctx = arg;
274 	struct spdk_thread *thread = spdk_get_thread();
275 	struct spdk_poller *poller;
276 
277 	spdk_json_write_object_begin(ctx->w);
278 	spdk_json_write_named_string(ctx->w, "name", spdk_thread_get_name(thread));
279 	spdk_json_write_named_uint64(ctx->w, "id", spdk_thread_get_id(thread));
280 
281 	spdk_json_write_named_array_begin(ctx->w, "active_pollers");
282 	for (poller = spdk_thread_get_first_active_poller(thread); poller != NULL;
283 	     poller = spdk_thread_get_next_active_poller(poller)) {
284 		rpc_get_poller(poller, ctx->w);
285 	}
286 	spdk_json_write_array_end(ctx->w);
287 
288 	spdk_json_write_named_array_begin(ctx->w, "timed_pollers");
289 	for (poller = spdk_thread_get_first_timed_poller(thread); poller != NULL;
290 	     poller = spdk_thread_get_next_timed_poller(poller)) {
291 		rpc_get_poller(poller, ctx->w);
292 	}
293 	spdk_json_write_array_end(ctx->w);
294 
295 	spdk_json_write_named_array_begin(ctx->w, "paused_pollers");
296 	for (poller = spdk_thread_get_first_paused_poller(thread); poller != NULL;
297 	     poller = spdk_thread_get_next_paused_poller(poller)) {
298 		rpc_get_poller(poller, ctx->w);
299 	}
300 	spdk_json_write_array_end(ctx->w);
301 
302 	spdk_json_write_object_end(ctx->w);
303 }
304 
305 static void
306 rpc_thread_get_pollers(struct spdk_jsonrpc_request *request,
307 		       const struct spdk_json_val *params)
308 {
309 	if (params) {
310 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
311 						 "'thread_get_pollers' requires no arguments");
312 		return;
313 	}
314 
315 	rpc_thread_get_stats_for_each(request, _rpc_thread_get_pollers);
316 }
317 
318 SPDK_RPC_REGISTER("thread_get_pollers", rpc_thread_get_pollers, SPDK_RPC_RUNTIME)
319 
320 static void
321 rpc_get_io_channel(struct spdk_io_channel *ch, struct spdk_json_write_ctx *w)
322 {
323 	spdk_json_write_object_begin(w);
324 	spdk_json_write_named_string(w, "name", spdk_io_device_get_name(ch->dev));
325 	spdk_json_write_named_uint32(w, "ref", spdk_io_channel_get_ref_count(ch));
326 	spdk_json_write_object_end(w);
327 }
328 
329 static void
330 _rpc_thread_get_io_channels(void *arg)
331 {
332 	struct rpc_get_stats_ctx *ctx = arg;
333 	struct spdk_thread *thread = spdk_get_thread();
334 	struct spdk_io_channel *ch;
335 
336 	spdk_json_write_object_begin(ctx->w);
337 	spdk_json_write_named_string(ctx->w, "name", spdk_thread_get_name(thread));
338 
339 	spdk_json_write_named_array_begin(ctx->w, "io_channels");
340 	for (ch = spdk_thread_get_first_io_channel(thread); ch != NULL;
341 	     ch = spdk_thread_get_next_io_channel(ch)) {
342 		rpc_get_io_channel(ch, ctx->w);
343 	}
344 	spdk_json_write_array_end(ctx->w);
345 
346 	spdk_json_write_object_end(ctx->w);
347 }
348 
349 static void
350 rpc_thread_get_io_channels(struct spdk_jsonrpc_request *request,
351 			   const struct spdk_json_val *params)
352 {
353 	if (params) {
354 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
355 						 "'thread_get_io_channels' requires no arguments");
356 		return;
357 	}
358 
359 	rpc_thread_get_stats_for_each(request, _rpc_thread_get_io_channels);
360 }
361 
362 SPDK_RPC_REGISTER("thread_get_io_channels", rpc_thread_get_io_channels, SPDK_RPC_RUNTIME);
363 
364 static void
365 rpc_framework_get_reactors_done(void *arg1, void *arg2)
366 {
367 	struct rpc_get_stats_ctx *ctx = arg1;
368 
369 	spdk_json_write_array_end(ctx->w);
370 	spdk_json_write_object_end(ctx->w);
371 	spdk_jsonrpc_end_result(ctx->request, ctx->w);
372 
373 	free(ctx);
374 }
375 
376 #define GET_DELTA(end, start)	(end >= start ? end - start : 0)
377 
378 static void
379 _rpc_framework_get_reactors(void *arg1, void *arg2)
380 {
381 	struct rpc_get_stats_ctx *ctx = arg1;
382 	uint32_t current_core;
383 	uint32_t curr_core_freq;
384 	struct spdk_reactor *reactor;
385 	struct spdk_lw_thread *lw_thread;
386 	struct spdk_thread *thread;
387 	struct spdk_governor *governor;
388 	struct spdk_governor_capabilities capabilities;
389 
390 	current_core = spdk_env_get_current_core();
391 	reactor = spdk_reactor_get(current_core);
392 
393 	assert(reactor != NULL);
394 
395 	spdk_json_write_object_begin(ctx->w);
396 	spdk_json_write_named_uint32(ctx->w, "lcore", current_core);
397 	spdk_json_write_named_uint64(ctx->w, "busy", reactor->busy_tsc);
398 	spdk_json_write_named_uint64(ctx->w, "idle", reactor->idle_tsc);
399 	governor = _spdk_governor_get();
400 	/* We need to check whether governor can return current core frequency. */
401 	if (governor->get_core_capabilities && governor->get_core_freqs) {
402 		governor->get_core_capabilities(current_core, &capabilities);
403 		if (capabilities.freq_getset) {
404 			/* Governor returns core freqs in kHz, we want MHz. */
405 			curr_core_freq = governor->get_core_curr_freq(current_core) / 1000;
406 			spdk_json_write_named_uint32(ctx->w, "core_freq", curr_core_freq);
407 		}
408 	}
409 
410 	spdk_json_write_named_array_begin(ctx->w, "lw_threads");
411 	TAILQ_FOREACH(lw_thread, &reactor->threads, link) {
412 		thread = spdk_thread_get_from_ctx(lw_thread);
413 
414 		spdk_json_write_object_begin(ctx->w);
415 		spdk_json_write_named_string(ctx->w, "name", spdk_thread_get_name(thread));
416 		spdk_json_write_named_uint64(ctx->w, "id", spdk_thread_get_id(thread));
417 		spdk_json_write_named_string(ctx->w, "cpumask",
418 					     spdk_cpuset_fmt(spdk_thread_get_cpumask(thread)));
419 		spdk_json_write_named_uint64(ctx->w, "elapsed",
420 					     GET_DELTA(ctx->now, lw_thread->tsc_start));
421 		spdk_json_write_object_end(ctx->w);
422 	}
423 	spdk_json_write_array_end(ctx->w);
424 
425 	spdk_json_write_object_end(ctx->w);
426 }
427 
428 static void
429 rpc_framework_get_reactors(struct spdk_jsonrpc_request *request,
430 			   const struct spdk_json_val *params)
431 {
432 	struct rpc_get_stats_ctx *ctx;
433 
434 	if (params) {
435 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
436 						 "`framework_get_reactors` requires no arguments");
437 		return;
438 	}
439 
440 	ctx = calloc(1, sizeof(*ctx));
441 	if (!ctx) {
442 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
443 						 "Memory allocation error");
444 		return;
445 	}
446 
447 	ctx->now = spdk_get_ticks();
448 	ctx->request = request;
449 	ctx->w = spdk_jsonrpc_begin_result(ctx->request);
450 
451 	spdk_json_write_object_begin(ctx->w);
452 	spdk_json_write_named_uint64(ctx->w, "tick_rate", spdk_get_ticks_hz());
453 	spdk_json_write_named_array_begin(ctx->w, "reactors");
454 
455 	spdk_for_each_reactor(_rpc_framework_get_reactors, ctx, NULL,
456 			      rpc_framework_get_reactors_done);
457 }
458 
459 SPDK_RPC_REGISTER("framework_get_reactors", rpc_framework_get_reactors, SPDK_RPC_RUNTIME)
460 
461 struct rpc_set_scheduler_ctx {
462 	char *name;
463 	uint64_t period;
464 };
465 
466 static void
467 free_rpc_framework_set_scheduler(struct rpc_set_scheduler_ctx *r)
468 {
469 	free(r->name);
470 }
471 
472 static const struct spdk_json_object_decoder rpc_set_scheduler_decoders[] = {
473 	{"name", offsetof(struct rpc_set_scheduler_ctx, name), spdk_json_decode_string},
474 	{"period", offsetof(struct rpc_set_scheduler_ctx, period), spdk_json_decode_uint64, true}
475 };
476 
477 static void
478 rpc_framework_set_scheduler(struct spdk_jsonrpc_request *request,
479 			    const struct spdk_json_val *params)
480 {
481 	struct rpc_set_scheduler_ctx req = {NULL};
482 	int ret;
483 
484 	ret = spdk_json_decode_object(params, rpc_set_scheduler_decoders,
485 				      SPDK_COUNTOF(rpc_set_scheduler_decoders),
486 				      &req);
487 	if (ret) {
488 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
489 						 "Invalid parameters");
490 		goto end;
491 	}
492 
493 	if (req.period != 0) {
494 		_spdk_scheduler_period_set(req.period);
495 	}
496 
497 	ret = _spdk_scheduler_set(req.name);
498 	if (ret) {
499 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
500 						 spdk_strerror(ret));
501 		goto end;
502 	}
503 
504 	spdk_jsonrpc_send_bool_response(request, true);
505 
506 end:
507 	free_rpc_framework_set_scheduler(&req);
508 }
509 SPDK_RPC_REGISTER("framework_set_scheduler", rpc_framework_set_scheduler, SPDK_RPC_STARTUP)
510 
511 static void
512 rpc_framework_get_scheduler(struct spdk_jsonrpc_request *request,
513 			    const struct spdk_json_val *params)
514 {
515 	struct spdk_json_write_ctx *w;
516 	struct spdk_scheduler *scheduler = _spdk_scheduler_get();
517 	uint64_t scheduler_period = _spdk_scheduler_period_get();
518 	struct spdk_governor *governor = _spdk_governor_get();
519 
520 	if (params) {
521 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
522 						 "'rpc_get_scheduler' requires no arguments");
523 		return;
524 	}
525 
526 	w = spdk_jsonrpc_begin_result(request);
527 	spdk_json_write_object_begin(w);
528 	spdk_json_write_named_string(w, "scheduler_name", scheduler->name);
529 	spdk_json_write_named_uint64(w, "scheduler_period", scheduler_period);
530 	spdk_json_write_named_string(w, "governor_name", governor->name);
531 	spdk_json_write_object_end(w);
532 	spdk_jsonrpc_end_result(request, w);
533 }
534 SPDK_RPC_REGISTER("framework_get_scheduler", rpc_framework_get_scheduler, SPDK_RPC_RUNTIME)
535 
536 struct rpc_thread_set_cpumask_ctx {
537 	struct spdk_jsonrpc_request *request;
538 	struct spdk_cpuset cpumask;
539 	int status;
540 	struct spdk_thread *orig_thread;
541 };
542 
543 static void
544 rpc_thread_set_cpumask_done(void *_ctx)
545 {
546 	struct rpc_thread_set_cpumask_ctx *ctx = _ctx;
547 
548 	if (ctx->status == 0) {
549 		spdk_jsonrpc_send_bool_response(ctx->request, true);
550 	} else {
551 		spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
552 						 spdk_strerror(-ctx->status));
553 	}
554 
555 	free(ctx);
556 }
557 
558 static void
559 _rpc_thread_set_cpumask(void *_ctx)
560 {
561 	struct rpc_thread_set_cpumask_ctx *ctx = _ctx;
562 
563 	ctx->status = spdk_thread_set_cpumask(&ctx->cpumask);
564 
565 	spdk_thread_send_msg(ctx->orig_thread, rpc_thread_set_cpumask_done, ctx);
566 }
567 
568 struct rpc_thread_set_cpumask {
569 	uint64_t id;
570 	char *cpumask;
571 };
572 
573 static const struct spdk_json_object_decoder rpc_thread_set_cpumask_decoders[] = {
574 	{"id", offsetof(struct rpc_thread_set_cpumask, id), spdk_json_decode_uint64},
575 	{"cpumask", offsetof(struct rpc_thread_set_cpumask, cpumask), spdk_json_decode_string},
576 };
577 
578 static void
579 rpc_thread_set_cpumask(struct spdk_jsonrpc_request *request,
580 		       const struct spdk_json_val *params)
581 {
582 	struct rpc_thread_set_cpumask req = {};
583 	struct rpc_thread_set_cpumask_ctx *ctx;
584 	const struct spdk_cpuset *coremask;
585 	struct spdk_cpuset tmp_mask;
586 	struct spdk_thread *thread;
587 	int rc;
588 
589 	ctx = calloc(1, sizeof(*ctx));
590 	if (ctx == NULL) {
591 		SPDK_ERRLOG("Memory allocation failed\n");
592 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
593 						 "Memory allocation failed");
594 		return;
595 	}
596 
597 	if (spdk_json_decode_object(params, rpc_thread_set_cpumask_decoders,
598 				    SPDK_COUNTOF(rpc_thread_set_cpumask_decoders),
599 				    &req)) {
600 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
601 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
602 						 "spdk_json_decode_object failed");
603 		goto err;
604 	}
605 
606 	thread = spdk_thread_get_by_id(req.id);
607 	if (thread == NULL) {
608 		SPDK_ERRLOG("Thread %" PRIu64 " does not exist\n", req.id);
609 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
610 						     "Thread %" PRIu64 " does not exist", req.id);
611 		goto err;
612 	}
613 
614 	rc = spdk_app_parse_core_mask(req.cpumask, &ctx->cpumask);
615 	if (rc != 0) {
616 		SPDK_ERRLOG("Invalid cpumask %s\n", req.cpumask);
617 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
618 						     "Invalid cpumask %s", req.cpumask);
619 		goto err;
620 	}
621 
622 	if (spdk_cpuset_count(&ctx->cpumask) == 0) {
623 		coremask = spdk_app_get_core_mask();
624 		spdk_cpuset_copy(&tmp_mask, coremask);
625 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
626 						     "No CPU is selected from reactor mask %s\n",
627 						     spdk_cpuset_fmt(&tmp_mask));
628 		goto err;
629 	}
630 
631 	/* There may be any reactors running in interrupt mode. But currently,
632 	 * when interrupt ability of the spdk_thread is not enabled,
633 	 * spdk_thread can't get executed on reactor which runs in interrupt.
634 	 * Exclude the situation that reactors specified by the cpumask are
635 	 * all in interrupt mode.
636 	 */
637 	if (!spdk_interrupt_mode_is_enabled()) {
638 		struct spdk_reactor *local_reactor = spdk_reactor_get(spdk_env_get_current_core());
639 		struct spdk_cpuset tmp_cpuset;
640 
641 		/* Masking off reactors which are in interrupt mode */
642 		spdk_cpuset_copy(&tmp_cpuset, &local_reactor->notify_cpuset);
643 		spdk_cpuset_negate(&tmp_cpuset);
644 		spdk_cpuset_and(&tmp_cpuset, &ctx->cpumask);
645 		if (spdk_cpuset_count(&tmp_cpuset) == 0) {
646 			spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
647 							     "cpumask %s are all in interrupt mode, and can't be scheduled yet\n",
648 							     req.cpumask);
649 			goto err;
650 		}
651 	}
652 
653 	ctx->request = request;
654 	ctx->orig_thread = spdk_get_thread();
655 
656 	spdk_thread_send_msg(thread, _rpc_thread_set_cpumask, ctx);
657 
658 	free(req.cpumask);
659 	return;
660 
661 err:
662 	free(req.cpumask);
663 	free(ctx);
664 }
665 SPDK_RPC_REGISTER("thread_set_cpumask", rpc_thread_set_cpumask, SPDK_RPC_RUNTIME)
666 SPDK_LOG_REGISTER_COMPONENT(app_rpc)
667