xref: /spdk/lib/init/subsystem_rpc.c (revision a2815831cc4c36d82d7e4f9938308d38d0f304bd)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   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/rpc.h"
35 #include "spdk/string.h"
36 #include "spdk/util.h"
37 #include "spdk/env.h"
38 #include "spdk/log.h"
39 
40 #include "spdk_internal/init.h"
41 
42 #include "subsystem.h"
43 
44 static void
45 rpc_framework_get_subsystems(struct spdk_jsonrpc_request *request,
46 			     const struct spdk_json_val *params)
47 {
48 	struct spdk_json_write_ctx *w;
49 	struct spdk_subsystem *subsystem;
50 	struct spdk_subsystem_depend *deps;
51 
52 	if (params) {
53 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
54 						 "'framework_get_subsystems' requires no arguments");
55 		return;
56 	}
57 
58 	w = spdk_jsonrpc_begin_result(request);
59 	spdk_json_write_array_begin(w);
60 	subsystem = subsystem_get_first();
61 	while (subsystem != NULL) {
62 		spdk_json_write_object_begin(w);
63 
64 		spdk_json_write_named_string(w, "subsystem", subsystem->name);
65 		spdk_json_write_named_array_begin(w, "depends_on");
66 		deps = subsystem_get_first_depend();
67 		while (deps != NULL) {
68 			if (strcmp(subsystem->name, deps->name) == 0) {
69 				spdk_json_write_string(w, deps->depends_on);
70 			}
71 			deps = subsystem_get_next_depend(deps);
72 		}
73 		spdk_json_write_array_end(w);
74 		spdk_json_write_object_end(w);
75 		subsystem = subsystem_get_next(subsystem);
76 	}
77 	spdk_json_write_array_end(w);
78 	spdk_jsonrpc_end_result(request, w);
79 }
80 
81 SPDK_RPC_REGISTER("framework_get_subsystems", rpc_framework_get_subsystems, SPDK_RPC_RUNTIME)
82 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_get_subsystems, get_subsystems)
83 
84 struct rpc_framework_get_config_ctx {
85 	char *name;
86 };
87 
88 static const struct spdk_json_object_decoder rpc_framework_get_config_ctx[] = {
89 	{"name", offsetof(struct rpc_framework_get_config_ctx, name), spdk_json_decode_string},
90 };
91 
92 static void
93 rpc_framework_get_config(struct spdk_jsonrpc_request *request,
94 			 const struct spdk_json_val *params)
95 {
96 	struct rpc_framework_get_config_ctx req = {};
97 	struct spdk_json_write_ctx *w;
98 	struct spdk_subsystem *subsystem;
99 
100 	if (spdk_json_decode_object(params, rpc_framework_get_config_ctx,
101 				    SPDK_COUNTOF(rpc_framework_get_config_ctx), &req)) {
102 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid arguments");
103 		return;
104 	}
105 
106 	subsystem = subsystem_find(req.name);
107 	if (!subsystem) {
108 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
109 						     "Subsystem '%s' not found", req.name);
110 		free(req.name);
111 		return;
112 	}
113 
114 	free(req.name);
115 
116 	w = spdk_jsonrpc_begin_result(request);
117 	subsystem_config_json(w, subsystem);
118 	spdk_jsonrpc_end_result(request, w);
119 }
120 
121 SPDK_RPC_REGISTER("framework_get_config", rpc_framework_get_config, SPDK_RPC_RUNTIME)
122 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_get_config, get_subsystem_config)
123 
124 static void
125 dump_pci_device(void *ctx, struct spdk_pci_device *dev)
126 {
127 	struct spdk_json_write_ctx *w = ctx;
128 	struct spdk_pci_addr addr;
129 	char config[4096], bdf[14];
130 	int rc;
131 
132 	addr = spdk_pci_device_get_addr(dev);
133 	spdk_pci_addr_fmt(bdf, sizeof(bdf), &addr);
134 
135 	rc = spdk_pci_device_cfg_read(dev, config, sizeof(config), 0);
136 	if (rc != 0) {
137 		SPDK_ERRLOG("Failed to read config space of device: %s\n", bdf);
138 		return;
139 	}
140 
141 	spdk_json_write_object_begin(w);
142 	spdk_json_write_named_string(w, "address", bdf);
143 
144 	/* Don't write the extended config space if it's all zeroes */
145 	if (spdk_mem_all_zero(&config[256], sizeof(config) - 256)) {
146 		spdk_json_write_named_bytearray(w, "config_space", config, 256);
147 	} else {
148 		spdk_json_write_named_bytearray(w, "config_space", config, sizeof(config));
149 	}
150 
151 	spdk_json_write_object_end(w);
152 }
153 
154 static void
155 rpc_framework_get_pci_devices(struct spdk_jsonrpc_request *request,
156 			      const struct spdk_json_val *params)
157 {
158 	struct spdk_json_write_ctx *w;
159 
160 	if (params != NULL) {
161 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
162 						 "framework_get_pci_devices doesn't accept any parameters.\n");
163 		return;
164 	}
165 
166 	w = spdk_jsonrpc_begin_result(request);
167 
168 	spdk_json_write_array_begin(w);
169 	spdk_pci_for_each_device(w, dump_pci_device);
170 	spdk_json_write_array_end(w);
171 
172 	spdk_jsonrpc_end_result(request, w);
173 }
174 SPDK_RPC_REGISTER("framework_get_pci_devices", rpc_framework_get_pci_devices, SPDK_RPC_RUNTIME)
175