blob: 51290b8d2a283cb2e6f54b4c5e501ce9935f4caf [file] [log] [blame]
Gunnar Millsac6a4442020-10-14 14:55:29 -05001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
Jonathan Doman1e1e5982021-06-11 09:36:17 -070019#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Jonathan Doman1e1e5982021-06-11 09:36:17 -070021#include "error_messages.hpp"
Gunnar Millsac6a4442020-10-14 14:55:29 -050022#include "health.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "query.hpp"
24#include "registries/privilege_registry.hpp"
25#include "utils/collection.hpp"
26#include "utils/dbus_utils.hpp"
27#include "utils/json_utils.hpp"
Gunnar Millsac6a4442020-10-14 14:55:29 -050028
29#include <boost/container/flat_map.hpp>
George Liue99073f2022-12-09 11:06:16 +080030#include <boost/system/error_code.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070031#include <sdbusplus/asio/property.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080032#include <sdbusplus/message/native_types.hpp>
Krzysztof Grobelny351053f2022-07-28 15:44:22 +020033#include <sdbusplus/unpack_properties.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080034#include <sdbusplus/utility/dedup_variant.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050035
George Liu7a1dbc42022-12-07 16:03:22 +080036#include <array>
37#include <string_view>
38
Gunnar Millsac6a4442020-10-14 14:55:29 -050039namespace redfish
40{
41
Jonathan Domanc9514482021-02-24 09:20:51 -080042// Interfaces which imply a D-Bus object represents a Processor
George Liu7a1dbc42022-12-07 16:03:22 +080043constexpr std::array<std::string_view, 2> processorInterfaces = {
Jonathan Domanc9514482021-02-24 09:20:51 -080044 "xyz.openbmc_project.Inventory.Item.Cpu",
45 "xyz.openbmc_project.Inventory.Item.Accelerator"};
Jonathan Doman2bab9832020-12-02 15:27:40 -080046
Sharad Yadav71b82f22021-05-10 15:11:39 +053047/**
48 * @brief Fill out uuid info of a processor by
49 * requesting data from the given D-Bus object.
50 *
51 * @param[in,out] aResp Async HTTP response.
52 * @param[in] service D-Bus service to query.
53 * @param[in] objPath D-Bus object to query.
54 */
55inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
56 const std::string& service,
57 const std::string& objPath)
58{
59 BMCWEB_LOG_DEBUG << "Get Processor UUID";
Jonathan Doman1e1e5982021-06-11 09:36:17 -070060 sdbusplus::asio::getProperty<std::string>(
61 *crow::connections::systemBus, service, objPath,
62 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080063 [objPath, aResp{std::move(aResp)}](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -070064 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -070065 if (ec)
66 {
67 BMCWEB_LOG_DEBUG << "DBUS response error";
68 messages::internalError(aResp->res);
69 return;
70 }
71 aResp->res.jsonValue["UUID"] = property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -070072 });
Sharad Yadav71b82f22021-05-10 15:11:39 +053073}
74
Ed Tanous711ac7a2021-12-20 09:34:41 -080075inline void getCpuDataByInterface(
76 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
77 const dbus::utility::DBusInteracesMap& cpuInterfacesProperties)
Gunnar Millsac6a4442020-10-14 14:55:29 -050078{
79 BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
80
Chicago Duana1649ec2021-03-30 16:54:58 +080081 // Set the default value of state
82 aResp->res.jsonValue["Status"]["State"] = "Enabled";
83 aResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -050084
85 for (const auto& interface : cpuInterfacesProperties)
86 {
87 for (const auto& property : interface.second)
88 {
Chicago Duana1649ec2021-03-30 16:54:58 +080089 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -050090 {
Chicago Duana1649ec2021-03-30 16:54:58 +080091 const bool* cpuPresent = std::get_if<bool>(&property.second);
92 if (cpuPresent == nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -050093 {
94 // Important property not in desired type
95 messages::internalError(aResp->res);
96 return;
97 }
Ed Tanouse05aec52022-01-25 10:28:56 -080098 if (!*cpuPresent)
Gunnar Millsac6a4442020-10-14 14:55:29 -050099 {
Chicago Duana1649ec2021-03-30 16:54:58 +0800100 // Slot is not populated
Gunnar Millsac6a4442020-10-14 14:55:29 -0500101 aResp->res.jsonValue["Status"]["State"] = "Absent";
Chicago Duana1649ec2021-03-30 16:54:58 +0800102 }
103 }
104 else if (property.first == "Functional")
105 {
106 const bool* cpuFunctional = std::get_if<bool>(&property.second);
107 if (cpuFunctional == nullptr)
108 {
109 messages::internalError(aResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500110 return;
111 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800112 if (!*cpuFunctional)
Chicago Duana1649ec2021-03-30 16:54:58 +0800113 {
114 aResp->res.jsonValue["Status"]["Health"] = "Critical";
115 }
116 }
117 else if (property.first == "CoreCount")
118 {
119 const uint16_t* coresCount =
120 std::get_if<uint16_t>(&property.second);
121 if (coresCount == nullptr)
122 {
123 messages::internalError(aResp->res);
124 return;
125 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500126 aResp->res.jsonValue["TotalCores"] = *coresCount;
127 }
Jonathan Domandc3fa662020-10-26 23:10:24 -0700128 else if (property.first == "MaxSpeedInMhz")
129 {
130 const uint32_t* value = std::get_if<uint32_t>(&property.second);
131 if (value != nullptr)
132 {
133 aResp->res.jsonValue["MaxSpeedMHz"] = *value;
134 }
135 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500136 else if (property.first == "Socket")
137 {
138 const std::string* value =
139 std::get_if<std::string>(&property.second);
140 if (value != nullptr)
141 {
142 aResp->res.jsonValue["Socket"] = *value;
143 }
144 }
145 else if (property.first == "ThreadCount")
146 {
Jonathan Domandc3fa662020-10-26 23:10:24 -0700147 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500148 if (value != nullptr)
149 {
150 aResp->res.jsonValue["TotalThreads"] = *value;
151 }
152 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700153 else if (property.first == "EffectiveFamily")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500154 {
Brandon Kim1930fbd2021-09-14 17:52:51 -0700155 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Brad Bishop6169de22022-09-14 13:08:32 -0400156 if (value != nullptr && *value != 2)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500157 {
158 aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800159 "0x" + intToHexString(*value, 4);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500160 }
161 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700162 else if (property.first == "EffectiveModel")
163 {
164 const uint16_t* value = std::get_if<uint16_t>(&property.second);
165 if (value == nullptr)
166 {
167 messages::internalError(aResp->res);
168 return;
169 }
Brad Bishop6169de22022-09-14 13:08:32 -0400170 if (*value != 0)
171 {
172 aResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
173 "0x" + intToHexString(*value, 4);
174 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700175 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500176 else if (property.first == "Id")
177 {
178 const uint64_t* value = std::get_if<uint64_t>(&property.second);
179 if (value != nullptr && *value != 0)
180 {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500181 aResp->res
182 .jsonValue["ProcessorId"]["IdentificationRegisters"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800183 "0x" + intToHexString(*value, 16);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500184 }
185 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700186 else if (property.first == "Microcode")
187 {
188 const uint32_t* value = std::get_if<uint32_t>(&property.second);
189 if (value == nullptr)
190 {
191 messages::internalError(aResp->res);
192 return;
193 }
Brad Bishop6169de22022-09-14 13:08:32 -0400194 if (*value != 0)
195 {
196 aResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
197 "0x" + intToHexString(*value, 8);
198 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700199 }
200 else if (property.first == "Step")
201 {
202 const uint16_t* value = std::get_if<uint16_t>(&property.second);
203 if (value == nullptr)
204 {
205 messages::internalError(aResp->res);
206 return;
207 }
Brad Bishop6169de22022-09-14 13:08:32 -0400208 if (*value != 0)
209 {
210 aResp->res.jsonValue["ProcessorId"]["Step"] =
211 "0x" + intToHexString(*value, 4);
212 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700213 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500214 }
215 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500216}
217
zhanghch058d1b46d2021-04-01 11:18:24 +0800218inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500219 const std::string& cpuId,
220 const std::string& service,
221 const std::string& objPath)
222{
223 BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
224
225 crow::connections::systemBus->async_method_call(
226 [cpuId, service, objPath, aResp{std::move(aResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800227 const boost::system::error_code& ec,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500228 const dbus::utility::ManagedObjectType& dbusData) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700229 if (ec)
230 {
231 BMCWEB_LOG_DEBUG << "DBUS response error";
232 messages::internalError(aResp->res);
233 return;
234 }
235 aResp->res.jsonValue["Id"] = cpuId;
236 aResp->res.jsonValue["Name"] = "Processor";
237 aResp->res.jsonValue["ProcessorType"] = "CPU";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500238
Ed Tanous002d39b2022-05-31 08:59:27 -0700239 bool slotPresent = false;
240 std::string corePath = objPath + "/core";
241 size_t totalCores = 0;
242 for (const auto& object : dbusData)
243 {
244 if (object.first.str == objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500245 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700246 getCpuDataByInterface(aResp, object.second);
247 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700248 else if (object.first.str.starts_with(corePath))
Ed Tanous002d39b2022-05-31 08:59:27 -0700249 {
250 for (const auto& interface : object.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500251 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700252 if (interface.first == "xyz.openbmc_project.Inventory.Item")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500253 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700254 for (const auto& property : interface.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500255 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700256 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500257 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700258 const bool* present =
259 std::get_if<bool>(&property.second);
260 if (present != nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500261 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700262 if (*present)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500263 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700264 slotPresent = true;
265 totalCores++;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500266 }
267 }
268 }
269 }
270 }
271 }
272 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700273 }
274 // In getCpuDataByInterface(), state and health are set
275 // based on the present and functional status. If core
276 // count is zero, then it has a higher precedence.
277 if (slotPresent)
278 {
279 if (totalCores == 0)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500280 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700281 // Slot is not populated, set status end return
282 aResp->res.jsonValue["Status"]["State"] = "Absent";
283 aResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500284 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700285 aResp->res.jsonValue["TotalCores"] = totalCores;
286 }
287 return;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500288 },
289 service, "/xyz/openbmc_project/inventory",
290 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
291}
292
zhanghch058d1b46d2021-04-01 11:18:24 +0800293inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500294 const std::string& service,
295 const std::string& objPath)
296{
297 BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200298 sdbusplus::asio::getAllProperties(
299 *crow::connections::systemBus, service, objPath,
300 "xyz.openbmc_project.Inventory.Decorator.Asset",
Gunnar Millsac6a4442020-10-14 14:55:29 -0500301 [objPath, aResp{std::move(aResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800302 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200303 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700304 if (ec)
305 {
306 BMCWEB_LOG_DEBUG << "DBUS response error";
307 messages::internalError(aResp->res);
308 return;
309 }
310
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200311 const std::string* serialNumber = nullptr;
312 const std::string* model = nullptr;
313 const std::string* manufacturer = nullptr;
314 const std::string* partNumber = nullptr;
315 const std::string* sparePartNumber = nullptr;
316
317 const bool success = sdbusplus::unpackPropertiesNoThrow(
318 dbus_utils::UnpackErrorPrinter(), properties, "SerialNumber",
319 serialNumber, "Model", model, "Manufacturer", manufacturer,
320 "PartNumber", partNumber, "SparePartNumber", sparePartNumber);
321
322 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700323 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200324 messages::internalError(aResp->res);
325 return;
326 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700327
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200328 if (serialNumber != nullptr && !serialNumber->empty())
329 {
330 aResp->res.jsonValue["SerialNumber"] = *serialNumber;
331 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700332
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200333 if ((model != nullptr) && !model->empty())
334 {
335 aResp->res.jsonValue["Model"] = *model;
336 }
337
338 if (manufacturer != nullptr)
339 {
340 aResp->res.jsonValue["Manufacturer"] = *manufacturer;
341
342 // Otherwise would be unexpected.
343 if (manufacturer->find("Intel") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700344 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200345 aResp->res.jsonValue["ProcessorArchitecture"] = "x86";
346 aResp->res.jsonValue["InstructionSet"] = "x86-64";
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200348 else if (manufacturer->find("IBM") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700349 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200350 aResp->res.jsonValue["ProcessorArchitecture"] = "Power";
351 aResp->res.jsonValue["InstructionSet"] = "PowerISA";
Ed Tanous002d39b2022-05-31 08:59:27 -0700352 }
353 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200354
355 if (partNumber != nullptr)
356 {
357 aResp->res.jsonValue["PartNumber"] = *partNumber;
358 }
359
Brad Bishop6169de22022-09-14 13:08:32 -0400360 if (sparePartNumber != nullptr && !sparePartNumber->empty())
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200361 {
362 aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
363 }
364 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500365}
366
zhanghch058d1b46d2021-04-01 11:18:24 +0800367inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500368 const std::string& service,
369 const std::string& objPath)
370{
371 BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200372 sdbusplus::asio::getAllProperties(
373 *crow::connections::systemBus, service, objPath,
374 "xyz.openbmc_project.Inventory.Decorator.Revision",
Gunnar Millsac6a4442020-10-14 14:55:29 -0500375 [objPath, aResp{std::move(aResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800376 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200377 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700378 if (ec)
379 {
380 BMCWEB_LOG_DEBUG << "DBUS response error";
381 messages::internalError(aResp->res);
382 return;
383 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500384
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200385 const std::string* version = nullptr;
386
387 const bool success = sdbusplus::unpackPropertiesNoThrow(
388 dbus_utils::UnpackErrorPrinter(), properties, "Version", version);
389
390 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700391 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200392 messages::internalError(aResp->res);
393 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700394 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200395
396 if (version != nullptr)
397 {
398 aResp->res.jsonValue["Version"] = *version;
399 }
400 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500401}
402
zhanghch058d1b46d2021-04-01 11:18:24 +0800403inline void getAcceleratorDataByService(
404 std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
405 const std::string& service, const std::string& objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500406{
407 BMCWEB_LOG_DEBUG
408 << "Get available system Accelerator resources by service.";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200409 sdbusplus::asio::getAllProperties(
410 *crow::connections::systemBus, service, objPath, "",
Gunnar Millsac6a4442020-10-14 14:55:29 -0500411 [acclrtrId, aResp{std::move(aResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800412 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200413 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700414 if (ec)
415 {
416 BMCWEB_LOG_DEBUG << "DBUS response error";
417 messages::internalError(aResp->res);
418 return;
419 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700420
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200421 const bool* functional = nullptr;
422 const bool* present = nullptr;
423
424 const bool success = sdbusplus::unpackPropertiesNoThrow(
425 dbus_utils::UnpackErrorPrinter(), properties, "Functional",
426 functional, "Present", present);
427
428 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700429 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200430 messages::internalError(aResp->res);
431 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700432 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500433
Ed Tanous002d39b2022-05-31 08:59:27 -0700434 std::string state = "Enabled";
435 std::string health = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500436
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200437 if (present != nullptr && !*present)
Ed Tanous002d39b2022-05-31 08:59:27 -0700438 {
439 state = "Absent";
440 }
441
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200442 if (functional != nullptr && !*functional)
Ed Tanous002d39b2022-05-31 08:59:27 -0700443 {
444 if (state == "Enabled")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500445 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700446 health = "Critical";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500447 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700448 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500449
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200450 aResp->res.jsonValue["Id"] = acclrtrId;
451 aResp->res.jsonValue["Name"] = "Processor";
Ed Tanous002d39b2022-05-31 08:59:27 -0700452 aResp->res.jsonValue["Status"]["State"] = state;
453 aResp->res.jsonValue["Status"]["Health"] = health;
454 aResp->res.jsonValue["ProcessorType"] = "Accelerator";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200455 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500456}
457
Jonathan Domandba0c292020-12-02 15:34:13 -0800458// OperatingConfig D-Bus Types
459using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
460using BaseSpeedPrioritySettingsProperty =
461 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
462// uint32_t and size_t may or may not be the same type, requiring a dedup'd
463// variant
Jonathan Domandba0c292020-12-02 15:34:13 -0800464
465/**
466 * Fill out the HighSpeedCoreIDs in a Processor resource from the given
467 * OperatingConfig D-Bus property.
468 *
469 * @param[in,out] aResp Async HTTP response.
470 * @param[in] baseSpeedSettings Full list of base speed priority groups,
471 * to use to determine the list of high
472 * speed cores.
473 */
474inline void highSpeedCoreIdsHandler(
zhanghch058d1b46d2021-04-01 11:18:24 +0800475 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800476 const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
477{
478 // The D-Bus property does not indicate which bucket is the "high
479 // priority" group, so let's discern that by looking for the one with
480 // highest base frequency.
481 auto highPriorityGroup = baseSpeedSettings.cend();
482 uint32_t highestBaseSpeed = 0;
483 for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
484 ++it)
485 {
486 const uint32_t baseFreq = std::get<uint32_t>(*it);
487 if (baseFreq > highestBaseSpeed)
488 {
489 highestBaseSpeed = baseFreq;
490 highPriorityGroup = it;
491 }
492 }
493
494 nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
495 jsonCoreIds = nlohmann::json::array();
496
497 // There may not be any entries in the D-Bus property, so only populate
498 // if there was actually something there.
499 if (highPriorityGroup != baseSpeedSettings.cend())
500 {
501 jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
502 }
503}
504
505/**
506 * Fill out OperatingConfig related items in a Processor resource by requesting
507 * data from the given D-Bus object.
508 *
509 * @param[in,out] aResp Async HTTP response.
510 * @param[in] cpuId CPU D-Bus name.
511 * @param[in] service D-Bus service to query.
512 * @param[in] objPath D-Bus object to query.
513 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800514inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800515 const std::string& cpuId,
516 const std::string& service,
517 const std::string& objPath)
518{
519 BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
520
521 // First, GetAll CurrentOperatingConfig properties on the object
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200522 sdbusplus::asio::getAllProperties(
523 *crow::connections::systemBus, service, objPath,
524 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
525 [aResp, cpuId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800526 service](const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200527 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700528 if (ec)
529 {
530 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
531 messages::internalError(aResp->res);
532 return;
533 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800534
Ed Tanous002d39b2022-05-31 08:59:27 -0700535 nlohmann::json& json = aResp->res.jsonValue;
Jonathan Domandba0c292020-12-02 15:34:13 -0800536
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200537 const sdbusplus::message::object_path* appliedConfig = nullptr;
538 const bool* baseSpeedPriorityEnabled = nullptr;
539
540 const bool success = sdbusplus::unpackPropertiesNoThrow(
541 dbus_utils::UnpackErrorPrinter(), properties, "AppliedConfig",
542 appliedConfig, "BaseSpeedPriorityEnabled",
543 baseSpeedPriorityEnabled);
544
545 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700546 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200547 messages::internalError(aResp->res);
548 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700549 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200550
551 if (appliedConfig != nullptr)
552 {
553 const std::string& dbusPath = appliedConfig->str;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200554 nlohmann::json::object_t operatingConfig;
Willy Tueddfc432022-09-26 16:46:38 +0000555 operatingConfig["@odata.id"] = crow::utility::urlFromPieces(
556 "redfish", "v1", "Systems", "system", "Processors", cpuId,
557 "OperatingConfigs");
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200558 json["OperatingConfigs"] = std::move(operatingConfig);
559
560 // Reuse the D-Bus config object name for the Redfish
561 // URI
562 size_t baseNamePos = dbusPath.rfind('/');
563 if (baseNamePos == std::string::npos ||
564 baseNamePos == (dbusPath.size() - 1))
565 {
566 // If the AppliedConfig was somehow not a valid path,
567 // skip adding any more properties, since everything
568 // else is tied to this applied config.
569 messages::internalError(aResp->res);
570 return;
571 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200572 nlohmann::json::object_t appliedOperatingConfig;
Willy Tueddfc432022-09-26 16:46:38 +0000573 appliedOperatingConfig["@odata.id"] = crow::utility::urlFromPieces(
574 "redfish", "v1", "Systems", "system", "Processors", cpuId,
575 "OperatingConfigs", dbusPath.substr(baseNamePos + 1));
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200576 json["AppliedOperatingConfig"] = std::move(appliedOperatingConfig);
577
578 // Once we found the current applied config, queue another
579 // request to read the base freq core ids out of that
580 // config.
581 sdbusplus::asio::getProperty<BaseSpeedPrioritySettingsProperty>(
582 *crow::connections::systemBus, service, dbusPath,
583 "xyz.openbmc_project.Inventory.Item.Cpu."
584 "OperatingConfig",
585 "BaseSpeedPrioritySettings",
586 [aResp](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800587 const boost::system::error_code& ec2,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200588 const BaseSpeedPrioritySettingsProperty& baseSpeedList) {
589 if (ec2)
590 {
591 BMCWEB_LOG_WARNING << "D-Bus Property Get error: " << ec2;
592 messages::internalError(aResp->res);
593 return;
594 }
595
596 highSpeedCoreIdsHandler(aResp, baseSpeedList);
597 });
598 }
599
600 if (baseSpeedPriorityEnabled != nullptr)
601 {
602 json["BaseSpeedPriorityState"] =
603 *baseSpeedPriorityEnabled ? "Enabled" : "Disabled";
604 }
605 });
Jonathan Domandba0c292020-12-02 15:34:13 -0800606}
607
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600608/**
609 * @brief Fill out location info of a processor by
610 * requesting data from the given D-Bus object.
611 *
612 * @param[in,out] aResp Async HTTP response.
613 * @param[in] service D-Bus service to query.
614 * @param[in] objPath D-Bus object to query.
615 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800616inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> aResp,
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600617 const std::string& service,
618 const std::string& objPath)
619{
620 BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700621 sdbusplus::asio::getProperty<std::string>(
622 *crow::connections::systemBus, service, objPath,
623 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800624 [objPath, aResp{std::move(aResp)}](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700625 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700626 if (ec)
627 {
628 BMCWEB_LOG_DEBUG << "DBUS response error";
629 messages::internalError(aResp->res);
630 return;
631 }
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600632
Ed Tanous002d39b2022-05-31 08:59:27 -0700633 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
634 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700635 });
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600636}
637
Jonathan Domanc9514482021-02-24 09:20:51 -0800638/**
Jonathan Doman49e429c2021-03-03 13:11:44 -0800639 * Populate the unique identifier in a Processor resource by requesting data
640 * from the given D-Bus object.
641 *
642 * @param[in,out] aResp Async HTTP response.
643 * @param[in] service D-Bus service to query.
644 * @param[in] objPath D-Bus object to query.
645 */
646inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
647 const std::string& service,
648 const std::string& objectPath)
649{
650 BMCWEB_LOG_DEBUG << "Get CPU UniqueIdentifier";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700651 sdbusplus::asio::getProperty<std::string>(
652 *crow::connections::systemBus, service, objectPath,
653 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
654 "UniqueIdentifier",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800655 [aResp](const boost::system::error_code& ec, const std::string& id) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 if (ec)
657 {
658 BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
659 messages::internalError(aResp->res);
660 return;
661 }
662 aResp->res.jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] =
663 id;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700664 });
Jonathan Doman49e429c2021-03-03 13:11:44 -0800665}
666
667/**
Jonathan Domanc9514482021-02-24 09:20:51 -0800668 * Find the D-Bus object representing the requested Processor, and call the
669 * handler with the results. If matching object is not found, add 404 error to
670 * response and don't call the handler.
671 *
672 * @param[in,out] resp Async HTTP response.
673 * @param[in] processorId Redfish Processor Id.
674 * @param[in] handler Callback to continue processing request upon
675 * successfully finding object.
676 */
677template <typename Handler>
zhanghch058d1b46d2021-04-01 11:18:24 +0800678inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800679 const std::string& processorId,
680 Handler&& handler)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500681{
682 BMCWEB_LOG_DEBUG << "Get available system processor resources.";
683
Jonathan Domanc9514482021-02-24 09:20:51 -0800684 // GetSubTree on all interfaces which provide info about a Processor
George Liue99073f2022-12-09 11:06:16 +0800685 constexpr std::array<std::string_view, 8> interfaces = {
686 "xyz.openbmc_project.Common.UUID",
687 "xyz.openbmc_project.Inventory.Decorator.Asset",
688 "xyz.openbmc_project.Inventory.Decorator.Revision",
689 "xyz.openbmc_project.Inventory.Item.Cpu",
690 "xyz.openbmc_project.Inventory.Decorator.LocationCode",
691 "xyz.openbmc_project.Inventory.Item.Accelerator",
692 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
693 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier"};
694 dbus::utility::getSubTree(
695 "/xyz/openbmc_project/inventory", 0, interfaces,
Jonathan Domanc9514482021-02-24 09:20:51 -0800696 [resp, processorId, handler = std::forward<Handler>(handler)](
George Liue99073f2022-12-09 11:06:16 +0800697 const boost::system::error_code& ec,
698 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700699 if (ec)
700 {
701 BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
702 messages::internalError(resp->res);
703 return;
704 }
705 for (const auto& [objectPath, serviceMap] : subtree)
706 {
707 // Ignore any objects which don't end with our desired cpu name
Ed Tanous11ba3972022-07-11 09:50:41 -0700708 if (!objectPath.ends_with(processorId))
Gunnar Millsac6a4442020-10-14 14:55:29 -0500709 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700710 continue;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500711 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700712
713 bool found = false;
714 // Filter out objects that don't have the CPU-specific
715 // interfaces to make sure we can return 404 on non-CPUs
716 // (e.g. /redfish/../Processors/dimm0)
717 for (const auto& [serviceName, interfaceList] : serviceMap)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500718 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700719 if (std::find_first_of(
720 interfaceList.begin(), interfaceList.end(),
721 processorInterfaces.begin(),
722 processorInterfaces.end()) != interfaceList.end())
Gunnar Millsac6a4442020-10-14 14:55:29 -0500723 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700724 found = true;
725 break;
Jonathan Doman2bab9832020-12-02 15:27:40 -0800726 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500727 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700728
729 if (!found)
730 {
731 continue;
732 }
733
734 // Process the first object which does match our cpu name and
735 // required interfaces, and potentially ignore any other
736 // matching objects. Assume all interfaces we want to process
737 // must be on the same object path.
738
Ed Tanous8a592812022-06-04 09:06:59 -0700739 handler(objectPath, serviceMap);
Ed Tanous002d39b2022-05-31 08:59:27 -0700740 return;
741 }
742 messages::resourceNotFound(resp->res, "Processor", processorId);
George Liue99073f2022-12-09 11:06:16 +0800743 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500744}
745
zhanghch058d1b46d2021-04-01 11:18:24 +0800746inline void getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800747 const std::string& processorId,
748 const std::string& objectPath,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600749 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Domanc9514482021-02-24 09:20:51 -0800750{
751 for (const auto& [serviceName, interfaceList] : serviceMap)
752 {
753 for (const auto& interface : interfaceList)
754 {
755 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
756 {
757 getCpuAssetData(aResp, serviceName, objectPath);
758 }
George Liu0fda0f12021-11-16 10:06:17 +0800759 else if (interface ==
760 "xyz.openbmc_project.Inventory.Decorator.Revision")
Jonathan Domanc9514482021-02-24 09:20:51 -0800761 {
762 getCpuRevisionData(aResp, serviceName, objectPath);
763 }
764 else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
765 {
766 getCpuDataByService(aResp, processorId, serviceName,
767 objectPath);
768 }
George Liu0fda0f12021-11-16 10:06:17 +0800769 else if (interface ==
770 "xyz.openbmc_project.Inventory.Item.Accelerator")
Jonathan Domanc9514482021-02-24 09:20:51 -0800771 {
772 getAcceleratorDataByService(aResp, processorId, serviceName,
773 objectPath);
774 }
George Liu0fda0f12021-11-16 10:06:17 +0800775 else if (
776 interface ==
777 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig")
Jonathan Domanc9514482021-02-24 09:20:51 -0800778 {
779 getCpuConfigData(aResp, processorId, serviceName, objectPath);
780 }
George Liu0fda0f12021-11-16 10:06:17 +0800781 else if (interface ==
782 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
Jonathan Domanc9514482021-02-24 09:20:51 -0800783 {
784 getCpuLocationCode(aResp, serviceName, objectPath);
785 }
Sharad Yadav71b82f22021-05-10 15:11:39 +0530786 else if (interface == "xyz.openbmc_project.Common.UUID")
787 {
788 getProcessorUUID(aResp, serviceName, objectPath);
789 }
George Liu0fda0f12021-11-16 10:06:17 +0800790 else if (interface ==
791 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier")
Jonathan Doman49e429c2021-03-03 13:11:44 -0800792 {
793 getCpuUniqueId(aResp, serviceName, objectPath);
794 }
Jonathan Domanc9514482021-02-24 09:20:51 -0800795 }
796 }
797}
798
Jonathan Domandba0c292020-12-02 15:34:13 -0800799/**
800 * Request all the properties for the given D-Bus object and fill out the
801 * related entries in the Redfish OperatingConfig response.
802 *
803 * @param[in,out] aResp Async HTTP response.
804 * @param[in] service D-Bus service name to query.
805 * @param[in] objPath D-Bus object to query.
806 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800807inline void
808 getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
809 const std::string& service,
810 const std::string& objPath)
Jonathan Domandba0c292020-12-02 15:34:13 -0800811{
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200812 sdbusplus::asio::getAllProperties(
813 *crow::connections::systemBus, service, objPath,
814 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800815 [aResp](const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200816 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700817 if (ec)
818 {
819 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
820 messages::internalError(aResp->res);
821 return;
822 }
823
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200824 const size_t* availableCoreCount = nullptr;
825 const uint32_t* baseSpeed = nullptr;
826 const uint32_t* maxJunctionTemperature = nullptr;
827 const uint32_t* maxSpeed = nullptr;
828 const uint32_t* powerLimit = nullptr;
829 const TurboProfileProperty* turboProfile = nullptr;
830 const BaseSpeedPrioritySettingsProperty* baseSpeedPrioritySettings =
831 nullptr;
832
833 const bool success = sdbusplus::unpackPropertiesNoThrow(
834 dbus_utils::UnpackErrorPrinter(), properties, "AvailableCoreCount",
835 availableCoreCount, "BaseSpeed", baseSpeed,
836 "MaxJunctionTemperature", maxJunctionTemperature, "MaxSpeed",
837 maxSpeed, "PowerLimit", powerLimit, "TurboProfile", turboProfile,
838 "BaseSpeedPrioritySettings", baseSpeedPrioritySettings);
839
840 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700841 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200842 messages::internalError(aResp->res);
843 return;
844 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700845
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200846 nlohmann::json& json = aResp->res.jsonValue;
Ed Tanous002d39b2022-05-31 08:59:27 -0700847
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200848 if (availableCoreCount != nullptr)
849 {
850 json["TotalAvailableCoreCount"] = *availableCoreCount;
851 }
852
853 if (baseSpeed != nullptr)
854 {
855 json["BaseSpeedMHz"] = *baseSpeed;
856 }
857
858 if (maxJunctionTemperature != nullptr)
859 {
860 json["MaxJunctionTemperatureCelsius"] = *maxJunctionTemperature;
861 }
862
863 if (maxSpeed != nullptr)
864 {
865 json["MaxSpeedMHz"] = *maxSpeed;
866 }
867
868 if (powerLimit != nullptr)
869 {
870 json["TDPWatts"] = *powerLimit;
871 }
872
873 if (turboProfile != nullptr)
874 {
875 nlohmann::json& turboArray = json["TurboProfile"];
876 turboArray = nlohmann::json::array();
877 for (const auto& [turboSpeed, coreCount] : *turboProfile)
878 {
879 nlohmann::json::object_t turbo;
880 turbo["ActiveCoreCount"] = coreCount;
881 turbo["MaxSpeedMHz"] = turboSpeed;
882 turboArray.push_back(std::move(turbo));
Ed Tanous002d39b2022-05-31 08:59:27 -0700883 }
884 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200885
886 if (baseSpeedPrioritySettings != nullptr)
887 {
888 nlohmann::json& baseSpeedArray = json["BaseSpeedPrioritySettings"];
889 baseSpeedArray = nlohmann::json::array();
890 for (const auto& [baseSpeedMhz, coreList] :
891 *baseSpeedPrioritySettings)
892 {
893 nlohmann::json::object_t speed;
894 speed["CoreCount"] = coreList.size();
895 speed["CoreIDs"] = coreList;
896 speed["BaseSpeedMHz"] = baseSpeedMhz;
897 baseSpeedArray.push_back(std::move(speed));
898 }
899 }
900 });
Jonathan Domandba0c292020-12-02 15:34:13 -0800901}
902
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800903/**
904 * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
905 * property. Main task is to translate error messages into Redfish errors.
906 *
907 * @param[in,out] resp HTTP response.
908 * @param[in] setPropVal Value which we attempted to set.
909 * @param[in] ec D-Bus response error code.
910 * @param[in] msg D-Bus response message.
911 */
912inline void
913 handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
914 const std::string& setPropVal,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800915 const boost::system::error_code& ec,
Patrick Williams59d494e2022-07-22 19:26:55 -0500916 const sdbusplus::message_t& msg)
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800917{
918 if (!ec)
919 {
920 BMCWEB_LOG_DEBUG << "Set Property succeeded";
921 return;
922 }
923
924 BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
925
926 const sd_bus_error* dbusError = msg.get_error();
927 if (dbusError == nullptr)
928 {
929 messages::internalError(resp->res);
930 return;
931 }
932
933 // The asio error code doesn't know about our custom errors, so we have to
934 // parse the error string. Some of these D-Bus -> Redfish translations are a
935 // stretch, but it's good to try to communicate something vaguely useful.
936 if (strcmp(dbusError->name,
937 "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
938 {
939 // Service did not like the object_path we tried to set.
940 messages::propertyValueIncorrect(
941 resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
942 }
943 else if (strcmp(dbusError->name,
944 "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
945 {
946 // Service indicates we can never change the config for this processor.
947 messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
948 }
949 else if (strcmp(dbusError->name,
950 "xyz.openbmc_project.Common.Error.Unavailable") == 0)
951 {
952 // Service indicates the config cannot be changed right now, but maybe
953 // in a different system state.
954 messages::resourceInStandby(resp->res);
955 }
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800956 else
957 {
958 messages::internalError(resp->res);
959 }
960}
961
962/**
963 * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
964 * validation of the input data, and then set the D-Bus property.
965 *
966 * @param[in,out] resp Async HTTP response.
967 * @param[in] processorId Processor's Id.
968 * @param[in] appliedConfigUri New property value to apply.
969 * @param[in] cpuObjectPath Path of CPU object to modify.
970 * @param[in] serviceMap Service map for CPU object.
971 */
972inline void patchAppliedOperatingConfig(
973 const std::shared_ptr<bmcweb::AsyncResp>& resp,
974 const std::string& processorId, const std::string& appliedConfigUri,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600975 const std::string& cpuObjectPath,
976 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800977{
978 // Check that the property even exists by checking for the interface
979 const std::string* controlService = nullptr;
980 for (const auto& [serviceName, interfaceList] : serviceMap)
981 {
982 if (std::find(interfaceList.begin(), interfaceList.end(),
983 "xyz.openbmc_project.Control.Processor."
984 "CurrentOperatingConfig") != interfaceList.end())
985 {
986 controlService = &serviceName;
987 break;
988 }
989 }
990
991 if (controlService == nullptr)
992 {
993 messages::internalError(resp->res);
994 return;
995 }
996
997 // Check that the config URI is a child of the cpu URI being patched.
998 std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
999 expectedPrefix += processorId;
1000 expectedPrefix += "/OperatingConfigs/";
Ed Tanous11ba3972022-07-11 09:50:41 -07001001 if (!appliedConfigUri.starts_with(expectedPrefix) ||
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001002 expectedPrefix.size() == appliedConfigUri.size())
1003 {
1004 messages::propertyValueIncorrect(
1005 resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
1006 return;
1007 }
1008
1009 // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
1010 // direct child of the CPU object.
1011 // Strip the expectedPrefix from the config URI to get the "filename", and
1012 // append to the CPU's path.
1013 std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
1014 sdbusplus::message::object_path configPath(cpuObjectPath);
1015 configPath /= configBaseName;
1016
1017 BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
1018
1019 // Set the property, with handler to check error responses
1020 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001021 [resp, appliedConfigUri](const boost::system::error_code& ec,
Patrick Williams59d494e2022-07-22 19:26:55 -05001022 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001023 handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001024 },
1025 *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
1026 "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Ed Tanous168e20c2021-12-13 14:39:53 -08001027 "AppliedConfig", dbus::utility::DbusVariantType(std::move(configPath)));
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001028}
1029
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001030inline void handleProcessorHead(crow::App& app, const crow::Request& req,
1031 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1032 const std::string& /* systemName */,
1033 const std::string& /* processorId */)
1034{
1035 if (!redfish::setUpRedfishRoute(app, req, aResp))
1036 {
1037 return;
1038 }
1039 aResp->res.addHeader(
1040 boost::beast::http::field::link,
1041 "</redfish/v1/JsonSchemas/Processor/Processor.json>; rel=describedby");
1042}
1043
1044inline void handleProcessorCollectionHead(
1045 crow::App& app, const crow::Request& req,
1046 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1047 const std::string& /* systemName */)
1048{
1049 if (!redfish::setUpRedfishRoute(app, req, aResp))
1050 {
1051 return;
1052 }
1053 aResp->res.addHeader(
1054 boost::beast::http::field::link,
1055 "</redfish/v1/JsonSchemas/ProcessorCollection/ProcessorCollection.json>; rel=describedby");
1056}
1057
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001058inline void requestRoutesOperatingConfigCollection(App& app)
Jonathan Domandba0c292020-12-02 15:34:13 -08001059{
Jonathan Domandba0c292020-12-02 15:34:13 -08001060
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001061 BMCWEB_ROUTE(
1062 app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
Ed Tanoused398212021-06-09 17:05:54 -07001063 .privileges(redfish::privileges::getOperatingConfigCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001064 .methods(boost::beast::http::verb::get)(
1065 [&app](const crow::Request& req,
1066 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1067 const std::string& cpuName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001068 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001069 {
1070 return;
1071 }
1072 asyncResp->res.jsonValue["@odata.type"] =
1073 "#OperatingConfigCollection.OperatingConfigCollection";
1074 asyncResp->res.jsonValue["@odata.id"] = req.url;
1075 asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
1076
1077 // First find the matching CPU object so we know how to
1078 // constrain our search for related Config objects.
George Liu7a1dbc42022-12-07 16:03:22 +08001079 const std::array<std::string_view, 1> interfaces = {
1080 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"};
1081 dbus::utility::getSubTreePaths(
1082 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001083 [asyncResp, cpuName](
George Liu7a1dbc42022-12-07 16:03:22 +08001084 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001085 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
1086 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001087 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001088 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1089 << ec.message();
1090 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001091 return;
1092 }
Jonathan Domandba0c292020-12-02 15:34:13 -08001093
Ed Tanous002d39b2022-05-31 08:59:27 -07001094 for (const std::string& object : objects)
1095 {
Ed Tanous11ba3972022-07-11 09:50:41 -07001096 if (!object.ends_with(cpuName))
Ed Tanous002d39b2022-05-31 08:59:27 -07001097 {
1098 continue;
1099 }
George Liu0fda0f12021-11-16 10:06:17 +08001100
Ed Tanous002d39b2022-05-31 08:59:27 -07001101 // Not expected that there will be multiple matching
1102 // CPU objects, but if there are just use the first
1103 // one.
Jonathan Domandba0c292020-12-02 15:34:13 -08001104
Ed Tanous002d39b2022-05-31 08:59:27 -07001105 // Use the common search routine to construct the
1106 // Collection of all Config objects under this CPU.
George Liu7a1dbc42022-12-07 16:03:22 +08001107 constexpr std::array<std::string_view, 1> interface {
1108 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"
1109 };
Ed Tanous002d39b2022-05-31 08:59:27 -07001110 collection_util::getCollectionMembers(
1111 asyncResp,
Willy Tuae9031f2022-09-27 05:48:07 +00001112 crow::utility::urlFromPieces("redfish", "v1", "Systems",
1113 "system", "Processors",
1114 cpuName, "OperatingConfigs"),
George Liu7a1dbc42022-12-07 16:03:22 +08001115 interface, object.c_str());
Ed Tanous002d39b2022-05-31 08:59:27 -07001116 return;
1117 }
George Liu7a1dbc42022-12-07 16:03:22 +08001118 });
George Liu0fda0f12021-11-16 10:06:17 +08001119 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001120}
1121
1122inline void requestRoutesOperatingConfig(App& app)
1123{
1124 BMCWEB_ROUTE(
1125 app,
1126 "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001127 .privileges(redfish::privileges::getOperatingConfig)
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 .methods(boost::beast::http::verb::get)(
1129 [&app](const crow::Request& req,
1130 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1131 const std::string& cpuName, const std::string& configName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001132 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001133 {
1134 return;
1135 }
1136 // Ask for all objects implementing OperatingConfig so we can search
1137 // for one with a matching name
George Liue99073f2022-12-09 11:06:16 +08001138 constexpr std::array<std::string_view, 1> interfaces = {
1139 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"};
1140 dbus::utility::getSubTree(
1141 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001142 [asyncResp, cpuName, configName, reqUrl{req.url}](
George Liue99073f2022-12-09 11:06:16 +08001143 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001144 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1145 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001146 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1148 << ec.message();
1149 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001150 return;
1151 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001152 const std::string expectedEnding = cpuName + '/' + configName;
1153 for (const auto& [objectPath, serviceMap] : subtree)
1154 {
1155 // Ignore any configs without matching cpuX/configY
Ed Tanous11ba3972022-07-11 09:50:41 -07001156 if (!objectPath.ends_with(expectedEnding) || serviceMap.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -07001157 {
1158 continue;
1159 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001160
Ed Tanous002d39b2022-05-31 08:59:27 -07001161 nlohmann::json& json = asyncResp->res.jsonValue;
1162 json["@odata.type"] = "#OperatingConfig.v1_0_0.OperatingConfig";
1163 json["@odata.id"] = reqUrl;
1164 json["Name"] = "Processor Profile";
1165 json["Id"] = configName;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001166
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 // Just use the first implementation of the object - not
1168 // expected that there would be multiple matching
1169 // services
1170 getOperatingConfigData(asyncResp, serviceMap.begin()->first,
1171 objectPath);
1172 return;
1173 }
1174 messages::resourceNotFound(asyncResp->res, "OperatingConfig",
1175 configName);
George Liue99073f2022-12-09 11:06:16 +08001176 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001177 });
1178}
Jonathan Domandba0c292020-12-02 15:34:13 -08001179
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001180inline void requestRoutesProcessorCollection(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001181{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001182 /**
1183 * Functions triggers appropriate requests on DBus
1184 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001185 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001186 .privileges(redfish::privileges::headProcessorCollection)
1187 .methods(boost::beast::http::verb::head)(
1188 std::bind_front(handleProcessorCollectionHead, std::ref(app)));
1189
1190 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
Ed Tanoused398212021-06-09 17:05:54 -07001191 .privileges(redfish::privileges::getProcessorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001192 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001193 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001194 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1195 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001196 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001197 {
1198 return;
1199 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001200 if (systemName != "system")
1201 {
1202 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1203 systemName);
1204 return;
1205 }
1206
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001207 asyncResp->res.addHeader(
1208 boost::beast::http::field::link,
1209 "</redfish/v1/JsonSchemas/ProcessorCollection/ProcessorCollection.json>; rel=describedby");
1210
Ed Tanous002d39b2022-05-31 08:59:27 -07001211 asyncResp->res.jsonValue["@odata.type"] =
1212 "#ProcessorCollection.ProcessorCollection";
1213 asyncResp->res.jsonValue["Name"] = "Processor Collection";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001214
Ed Tanous002d39b2022-05-31 08:59:27 -07001215 asyncResp->res.jsonValue["@odata.id"] =
1216 "/redfish/v1/Systems/system/Processors";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001217
Ed Tanous002d39b2022-05-31 08:59:27 -07001218 collection_util::getCollectionMembers(
Willy Tuae9031f2022-09-27 05:48:07 +00001219 asyncResp,
1220 boost::urls::url("/redfish/v1/Systems/system/Processors"),
George Liu7a1dbc42022-12-07 16:03:22 +08001221 processorInterfaces);
Ed Tanous002d39b2022-05-31 08:59:27 -07001222 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001223}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001224
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001225inline void requestRoutesProcessor(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001226{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001227 /**
1228 * Functions triggers appropriate requests on DBus
1229 */
Gunnar Millsac6a4442020-10-14 14:55:29 -05001230
Ed Tanous22d268c2022-05-19 09:39:07 -07001231 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001232 .privileges(redfish::privileges::headProcessor)
1233 .methods(boost::beast::http::verb::head)(
1234 std::bind_front(handleProcessorHead, std::ref(app)));
1235
1236 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001237 .privileges(redfish::privileges::getProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001238 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001239 [&app](const crow::Request& req,
1240 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001241 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001242 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001243 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 {
1245 return;
1246 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001247 if (systemName != "system")
1248 {
1249 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1250 systemName);
1251 return;
1252 }
1253
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001254 asyncResp->res.addHeader(
1255 boost::beast::http::field::link,
1256 "</redfish/v1/JsonSchemas/Processor/Processor.json>; rel=describedby");
Ed Tanous002d39b2022-05-31 08:59:27 -07001257 asyncResp->res.jsonValue["@odata.type"] =
1258 "#Processor.v1_11_0.Processor";
Willy Tueddfc432022-09-26 16:46:38 +00001259 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
1260 "redfish", "v1", "Systems", "system", "Processors", processorId);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001261
Ed Tanous8a592812022-06-04 09:06:59 -07001262 getProcessorObject(
1263 asyncResp, processorId,
1264 std::bind_front(getProcessorData, asyncResp, processorId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001265 });
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001266
Ed Tanous22d268c2022-05-19 09:39:07 -07001267 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001268 .privileges(redfish::privileges::patchProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001269 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001270 [&app](const crow::Request& req,
1271 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001272 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001273 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001274 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001275 {
1276 return;
1277 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001278 if (systemName != "system")
1279 {
1280 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1281 systemName);
1282 return;
1283 }
1284
Ed Tanous002d39b2022-05-31 08:59:27 -07001285 std::optional<nlohmann::json> appliedConfigJson;
1286 if (!json_util::readJsonPatch(req, asyncResp->res,
1287 "AppliedOperatingConfig",
1288 appliedConfigJson))
1289 {
1290 return;
1291 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001292
Ed Tanous002d39b2022-05-31 08:59:27 -07001293 if (appliedConfigJson)
1294 {
Ed Tanousf8fe53e2022-06-30 15:55:45 -07001295 std::string appliedConfigUri;
Ed Tanous002d39b2022-05-31 08:59:27 -07001296 if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
1297 "@odata.id", appliedConfigUri))
1298 {
1299 return;
1300 }
1301 // Check for 404 and find matching D-Bus object, then run
1302 // property patch handlers if that all succeeds.
Ed Tanous8a592812022-06-04 09:06:59 -07001303 getProcessorObject(asyncResp, processorId,
1304 std::bind_front(patchAppliedOperatingConfig,
1305 asyncResp, processorId,
1306 appliedConfigUri));
Ed Tanous002d39b2022-05-31 08:59:27 -07001307 }
1308 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001309}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001310
1311} // namespace redfish