blob: ea09231fa3125ac63818812072f42996e830c437 [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
Jonathan Doman1e1e5982021-06-11 09:36:17 -070018#include "dbus_singleton.hpp"
19#include "error_messages.hpp"
Gunnar Millsac6a4442020-10-14 14:55:29 -050020#include "health.hpp"
21
John Edward Broadbent7e860f12021-04-08 15:57:16 -070022#include <app.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050023#include <boost/container/flat_map.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080024#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070025#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070026#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070027#include <sdbusplus/asio/property.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080028#include <sdbusplus/message/native_types.hpp>
Krzysztof Grobelny351053f2022-07-28 15:44:22 +020029#include <sdbusplus/unpack_properties.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080030#include <sdbusplus/utility/dedup_variant.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050031#include <utils/collection.hpp>
Krzysztof Grobelny351053f2022-07-28 15:44:22 +020032#include <utils/dbus_utils.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050033#include <utils/json_utils.hpp>
34
35namespace redfish
36{
37
Jonathan Domanc9514482021-02-24 09:20:51 -080038// Interfaces which imply a D-Bus object represents a Processor
39constexpr std::array<const char*, 2> processorInterfaces = {
40 "xyz.openbmc_project.Inventory.Item.Cpu",
41 "xyz.openbmc_project.Inventory.Item.Accelerator"};
Jonathan Doman2bab9832020-12-02 15:27:40 -080042
Sharad Yadav71b82f22021-05-10 15:11:39 +053043/**
44 * @brief Fill out uuid info of a processor by
45 * requesting data from the given D-Bus object.
46 *
47 * @param[in,out] aResp Async HTTP response.
48 * @param[in] service D-Bus service to query.
49 * @param[in] objPath D-Bus object to query.
50 */
51inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
52 const std::string& service,
53 const std::string& objPath)
54{
55 BMCWEB_LOG_DEBUG << "Get Processor UUID";
Jonathan Doman1e1e5982021-06-11 09:36:17 -070056 sdbusplus::asio::getProperty<std::string>(
57 *crow::connections::systemBus, service, objPath,
58 "xyz.openbmc_project.Common.UUID", "UUID",
59 [objPath, aResp{std::move(aResp)}](const boost::system::error_code ec,
60 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -070061 if (ec)
62 {
63 BMCWEB_LOG_DEBUG << "DBUS response error";
64 messages::internalError(aResp->res);
65 return;
66 }
67 aResp->res.jsonValue["UUID"] = property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -070068 });
Sharad Yadav71b82f22021-05-10 15:11:39 +053069}
70
Ed Tanous711ac7a2021-12-20 09:34:41 -080071inline void getCpuDataByInterface(
72 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
73 const dbus::utility::DBusInteracesMap& cpuInterfacesProperties)
Gunnar Millsac6a4442020-10-14 14:55:29 -050074{
75 BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
76
Chicago Duana1649ec2021-03-30 16:54:58 +080077 // Set the default value of state
78 aResp->res.jsonValue["Status"]["State"] = "Enabled";
79 aResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -050080
81 for (const auto& interface : cpuInterfacesProperties)
82 {
83 for (const auto& property : interface.second)
84 {
Chicago Duana1649ec2021-03-30 16:54:58 +080085 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -050086 {
Chicago Duana1649ec2021-03-30 16:54:58 +080087 const bool* cpuPresent = std::get_if<bool>(&property.second);
88 if (cpuPresent == nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -050089 {
90 // Important property not in desired type
91 messages::internalError(aResp->res);
92 return;
93 }
Ed Tanouse05aec52022-01-25 10:28:56 -080094 if (!*cpuPresent)
Gunnar Millsac6a4442020-10-14 14:55:29 -050095 {
Chicago Duana1649ec2021-03-30 16:54:58 +080096 // Slot is not populated
Gunnar Millsac6a4442020-10-14 14:55:29 -050097 aResp->res.jsonValue["Status"]["State"] = "Absent";
Chicago Duana1649ec2021-03-30 16:54:58 +080098 }
99 }
100 else if (property.first == "Functional")
101 {
102 const bool* cpuFunctional = std::get_if<bool>(&property.second);
103 if (cpuFunctional == nullptr)
104 {
105 messages::internalError(aResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500106 return;
107 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800108 if (!*cpuFunctional)
Chicago Duana1649ec2021-03-30 16:54:58 +0800109 {
110 aResp->res.jsonValue["Status"]["Health"] = "Critical";
111 }
112 }
113 else if (property.first == "CoreCount")
114 {
115 const uint16_t* coresCount =
116 std::get_if<uint16_t>(&property.second);
117 if (coresCount == nullptr)
118 {
119 messages::internalError(aResp->res);
120 return;
121 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500122 aResp->res.jsonValue["TotalCores"] = *coresCount;
123 }
Jonathan Domandc3fa662020-10-26 23:10:24 -0700124 else if (property.first == "MaxSpeedInMhz")
125 {
126 const uint32_t* value = std::get_if<uint32_t>(&property.second);
127 if (value != nullptr)
128 {
129 aResp->res.jsonValue["MaxSpeedMHz"] = *value;
130 }
131 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500132 else if (property.first == "Socket")
133 {
134 const std::string* value =
135 std::get_if<std::string>(&property.second);
136 if (value != nullptr)
137 {
138 aResp->res.jsonValue["Socket"] = *value;
139 }
140 }
141 else if (property.first == "ThreadCount")
142 {
Jonathan Domandc3fa662020-10-26 23:10:24 -0700143 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500144 if (value != nullptr)
145 {
146 aResp->res.jsonValue["TotalThreads"] = *value;
147 }
148 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700149 else if (property.first == "EffectiveFamily")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500150 {
Brandon Kim1930fbd2021-09-14 17:52:51 -0700151 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500152 if (value != nullptr)
153 {
154 aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800155 "0x" + intToHexString(*value, 4);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500156 }
157 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700158 else if (property.first == "EffectiveModel")
159 {
160 const uint16_t* value = std::get_if<uint16_t>(&property.second);
161 if (value == nullptr)
162 {
163 messages::internalError(aResp->res);
164 return;
165 }
166 aResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800167 "0x" + intToHexString(*value, 4);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700168 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500169 else if (property.first == "Id")
170 {
171 const uint64_t* value = std::get_if<uint64_t>(&property.second);
172 if (value != nullptr && *value != 0)
173 {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500174 aResp->res
175 .jsonValue["ProcessorId"]["IdentificationRegisters"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800176 "0x" + intToHexString(*value, 16);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500177 }
178 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700179 else if (property.first == "Microcode")
180 {
181 const uint32_t* value = std::get_if<uint32_t>(&property.second);
182 if (value == nullptr)
183 {
184 messages::internalError(aResp->res);
185 return;
186 }
187 aResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800188 "0x" + intToHexString(*value, 8);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700189 }
190 else if (property.first == "Step")
191 {
192 const uint16_t* value = std::get_if<uint16_t>(&property.second);
193 if (value == nullptr)
194 {
195 messages::internalError(aResp->res);
196 return;
197 }
198 aResp->res.jsonValue["ProcessorId"]["Step"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800199 "0x" + intToHexString(*value, 4);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700200 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500201 }
202 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500203}
204
zhanghch058d1b46d2021-04-01 11:18:24 +0800205inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500206 const std::string& cpuId,
207 const std::string& service,
208 const std::string& objPath)
209{
210 BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
211
212 crow::connections::systemBus->async_method_call(
213 [cpuId, service, objPath, aResp{std::move(aResp)}](
214 const boost::system::error_code ec,
215 const dbus::utility::ManagedObjectType& dbusData) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700216 if (ec)
217 {
218 BMCWEB_LOG_DEBUG << "DBUS response error";
219 messages::internalError(aResp->res);
220 return;
221 }
222 aResp->res.jsonValue["Id"] = cpuId;
223 aResp->res.jsonValue["Name"] = "Processor";
224 aResp->res.jsonValue["ProcessorType"] = "CPU";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500225
Ed Tanous002d39b2022-05-31 08:59:27 -0700226 bool slotPresent = false;
227 std::string corePath = objPath + "/core";
228 size_t totalCores = 0;
229 for (const auto& object : dbusData)
230 {
231 if (object.first.str == objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500232 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700233 getCpuDataByInterface(aResp, object.second);
234 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700235 else if (object.first.str.starts_with(corePath))
Ed Tanous002d39b2022-05-31 08:59:27 -0700236 {
237 for (const auto& interface : object.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500238 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700239 if (interface.first == "xyz.openbmc_project.Inventory.Item")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500240 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700241 for (const auto& property : interface.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500242 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700243 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500244 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700245 const bool* present =
246 std::get_if<bool>(&property.second);
247 if (present != nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500248 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700249 if (*present)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500250 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700251 slotPresent = true;
252 totalCores++;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500253 }
254 }
255 }
256 }
257 }
258 }
259 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700260 }
261 // In getCpuDataByInterface(), state and health are set
262 // based on the present and functional status. If core
263 // count is zero, then it has a higher precedence.
264 if (slotPresent)
265 {
266 if (totalCores == 0)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500267 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700268 // Slot is not populated, set status end return
269 aResp->res.jsonValue["Status"]["State"] = "Absent";
270 aResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500271 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700272 aResp->res.jsonValue["TotalCores"] = totalCores;
273 }
274 return;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500275 },
276 service, "/xyz/openbmc_project/inventory",
277 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
278}
279
zhanghch058d1b46d2021-04-01 11:18:24 +0800280inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500281 const std::string& service,
282 const std::string& objPath)
283{
284 BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200285 sdbusplus::asio::getAllProperties(
286 *crow::connections::systemBus, service, objPath,
287 "xyz.openbmc_project.Inventory.Decorator.Asset",
Gunnar Millsac6a4442020-10-14 14:55:29 -0500288 [objPath, aResp{std::move(aResp)}](
289 const boost::system::error_code ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200290 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700291 if (ec)
292 {
293 BMCWEB_LOG_DEBUG << "DBUS response error";
294 messages::internalError(aResp->res);
295 return;
296 }
297
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200298 const std::string* serialNumber = nullptr;
299 const std::string* model = nullptr;
300 const std::string* manufacturer = nullptr;
301 const std::string* partNumber = nullptr;
302 const std::string* sparePartNumber = nullptr;
303
304 const bool success = sdbusplus::unpackPropertiesNoThrow(
305 dbus_utils::UnpackErrorPrinter(), properties, "SerialNumber",
306 serialNumber, "Model", model, "Manufacturer", manufacturer,
307 "PartNumber", partNumber, "SparePartNumber", sparePartNumber);
308
309 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700310 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200311 messages::internalError(aResp->res);
312 return;
313 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700314
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200315 if (serialNumber != nullptr && !serialNumber->empty())
316 {
317 aResp->res.jsonValue["SerialNumber"] = *serialNumber;
318 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700319
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200320 if ((model != nullptr) && !model->empty())
321 {
322 aResp->res.jsonValue["Model"] = *model;
323 }
324
325 if (manufacturer != nullptr)
326 {
327 aResp->res.jsonValue["Manufacturer"] = *manufacturer;
328
329 // Otherwise would be unexpected.
330 if (manufacturer->find("Intel") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700331 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200332 aResp->res.jsonValue["ProcessorArchitecture"] = "x86";
333 aResp->res.jsonValue["InstructionSet"] = "x86-64";
Ed Tanous002d39b2022-05-31 08:59:27 -0700334 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200335 else if (manufacturer->find("IBM") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700336 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200337 aResp->res.jsonValue["ProcessorArchitecture"] = "Power";
338 aResp->res.jsonValue["InstructionSet"] = "PowerISA";
Ed Tanous002d39b2022-05-31 08:59:27 -0700339 }
340 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200341
342 if (partNumber != nullptr)
343 {
344 aResp->res.jsonValue["PartNumber"] = *partNumber;
345 }
346
347 if (sparePartNumber != nullptr)
348 {
349 aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
350 }
351 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500352}
353
zhanghch058d1b46d2021-04-01 11:18:24 +0800354inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500355 const std::string& service,
356 const std::string& objPath)
357{
358 BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200359 sdbusplus::asio::getAllProperties(
360 *crow::connections::systemBus, service, objPath,
361 "xyz.openbmc_project.Inventory.Decorator.Revision",
Gunnar Millsac6a4442020-10-14 14:55:29 -0500362 [objPath, aResp{std::move(aResp)}](
363 const boost::system::error_code ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200364 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700365 if (ec)
366 {
367 BMCWEB_LOG_DEBUG << "DBUS response error";
368 messages::internalError(aResp->res);
369 return;
370 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500371
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200372 const std::string* version = nullptr;
373
374 const bool success = sdbusplus::unpackPropertiesNoThrow(
375 dbus_utils::UnpackErrorPrinter(), properties, "Version", version);
376
377 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700378 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200379 messages::internalError(aResp->res);
380 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700381 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200382
383 if (version != nullptr)
384 {
385 aResp->res.jsonValue["Version"] = *version;
386 }
387 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500388}
389
zhanghch058d1b46d2021-04-01 11:18:24 +0800390inline void getAcceleratorDataByService(
391 std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
392 const std::string& service, const std::string& objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500393{
394 BMCWEB_LOG_DEBUG
395 << "Get available system Accelerator resources by service.";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200396 sdbusplus::asio::getAllProperties(
397 *crow::connections::systemBus, service, objPath, "",
Gunnar Millsac6a4442020-10-14 14:55:29 -0500398 [acclrtrId, aResp{std::move(aResp)}](
399 const boost::system::error_code ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200400 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700401 if (ec)
402 {
403 BMCWEB_LOG_DEBUG << "DBUS response error";
404 messages::internalError(aResp->res);
405 return;
406 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700407
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200408 const bool* functional = nullptr;
409 const bool* present = nullptr;
410
411 const bool success = sdbusplus::unpackPropertiesNoThrow(
412 dbus_utils::UnpackErrorPrinter(), properties, "Functional",
413 functional, "Present", present);
414
415 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700416 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200417 messages::internalError(aResp->res);
418 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700419 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500420
Ed Tanous002d39b2022-05-31 08:59:27 -0700421 std::string state = "Enabled";
422 std::string health = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500423
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200424 if (present != nullptr && !*present)
Ed Tanous002d39b2022-05-31 08:59:27 -0700425 {
426 state = "Absent";
427 }
428
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200429 if (functional != nullptr && !*functional)
Ed Tanous002d39b2022-05-31 08:59:27 -0700430 {
431 if (state == "Enabled")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500432 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700433 health = "Critical";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500434 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700435 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500436
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200437 aResp->res.jsonValue["Id"] = acclrtrId;
438 aResp->res.jsonValue["Name"] = "Processor";
Ed Tanous002d39b2022-05-31 08:59:27 -0700439 aResp->res.jsonValue["Status"]["State"] = state;
440 aResp->res.jsonValue["Status"]["Health"] = health;
441 aResp->res.jsonValue["ProcessorType"] = "Accelerator";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200442 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500443}
444
Jonathan Domandba0c292020-12-02 15:34:13 -0800445// OperatingConfig D-Bus Types
446using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
447using BaseSpeedPrioritySettingsProperty =
448 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
449// uint32_t and size_t may or may not be the same type, requiring a dedup'd
450// variant
Jonathan Domandba0c292020-12-02 15:34:13 -0800451
452/**
453 * Fill out the HighSpeedCoreIDs in a Processor resource from the given
454 * OperatingConfig D-Bus property.
455 *
456 * @param[in,out] aResp Async HTTP response.
457 * @param[in] baseSpeedSettings Full list of base speed priority groups,
458 * to use to determine the list of high
459 * speed cores.
460 */
461inline void highSpeedCoreIdsHandler(
zhanghch058d1b46d2021-04-01 11:18:24 +0800462 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800463 const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
464{
465 // The D-Bus property does not indicate which bucket is the "high
466 // priority" group, so let's discern that by looking for the one with
467 // highest base frequency.
468 auto highPriorityGroup = baseSpeedSettings.cend();
469 uint32_t highestBaseSpeed = 0;
470 for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
471 ++it)
472 {
473 const uint32_t baseFreq = std::get<uint32_t>(*it);
474 if (baseFreq > highestBaseSpeed)
475 {
476 highestBaseSpeed = baseFreq;
477 highPriorityGroup = it;
478 }
479 }
480
481 nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
482 jsonCoreIds = nlohmann::json::array();
483
484 // There may not be any entries in the D-Bus property, so only populate
485 // if there was actually something there.
486 if (highPriorityGroup != baseSpeedSettings.cend())
487 {
488 jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
489 }
490}
491
492/**
493 * Fill out OperatingConfig related items in a Processor resource by requesting
494 * data from the given D-Bus object.
495 *
496 * @param[in,out] aResp Async HTTP response.
497 * @param[in] cpuId CPU D-Bus name.
498 * @param[in] service D-Bus service to query.
499 * @param[in] objPath D-Bus object to query.
500 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800501inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800502 const std::string& cpuId,
503 const std::string& service,
504 const std::string& objPath)
505{
506 BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
507
508 // First, GetAll CurrentOperatingConfig properties on the object
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200509 sdbusplus::asio::getAllProperties(
510 *crow::connections::systemBus, service, objPath,
511 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
512 [aResp, cpuId,
513 service](const boost::system::error_code ec,
514 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700515 if (ec)
516 {
517 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
518 messages::internalError(aResp->res);
519 return;
520 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800521
Ed Tanous002d39b2022-05-31 08:59:27 -0700522 nlohmann::json& json = aResp->res.jsonValue;
Jonathan Domandba0c292020-12-02 15:34:13 -0800523
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200524 const sdbusplus::message::object_path* appliedConfig = nullptr;
525 const bool* baseSpeedPriorityEnabled = nullptr;
526
527 const bool success = sdbusplus::unpackPropertiesNoThrow(
528 dbus_utils::UnpackErrorPrinter(), properties, "AppliedConfig",
529 appliedConfig, "BaseSpeedPriorityEnabled",
530 baseSpeedPriorityEnabled);
531
532 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700533 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200534 messages::internalError(aResp->res);
535 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700536 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200537
538 if (appliedConfig != nullptr)
539 {
540 const std::string& dbusPath = appliedConfig->str;
541 std::string uri = "/redfish/v1/Systems/system/Processors/" + cpuId +
542 "/OperatingConfigs";
543 nlohmann::json::object_t operatingConfig;
544 operatingConfig["@odata.id"] = uri;
545 json["OperatingConfigs"] = std::move(operatingConfig);
546
547 // Reuse the D-Bus config object name for the Redfish
548 // URI
549 size_t baseNamePos = dbusPath.rfind('/');
550 if (baseNamePos == std::string::npos ||
551 baseNamePos == (dbusPath.size() - 1))
552 {
553 // If the AppliedConfig was somehow not a valid path,
554 // skip adding any more properties, since everything
555 // else is tied to this applied config.
556 messages::internalError(aResp->res);
557 return;
558 }
559 uri += '/';
560 uri += dbusPath.substr(baseNamePos + 1);
561 nlohmann::json::object_t appliedOperatingConfig;
562 appliedOperatingConfig["@odata.id"] = uri;
563 json["AppliedOperatingConfig"] = std::move(appliedOperatingConfig);
564
565 // Once we found the current applied config, queue another
566 // request to read the base freq core ids out of that
567 // config.
568 sdbusplus::asio::getProperty<BaseSpeedPrioritySettingsProperty>(
569 *crow::connections::systemBus, service, dbusPath,
570 "xyz.openbmc_project.Inventory.Item.Cpu."
571 "OperatingConfig",
572 "BaseSpeedPrioritySettings",
573 [aResp](
574 const boost::system::error_code ec2,
575 const BaseSpeedPrioritySettingsProperty& baseSpeedList) {
576 if (ec2)
577 {
578 BMCWEB_LOG_WARNING << "D-Bus Property Get error: " << ec2;
579 messages::internalError(aResp->res);
580 return;
581 }
582
583 highSpeedCoreIdsHandler(aResp, baseSpeedList);
584 });
585 }
586
587 if (baseSpeedPriorityEnabled != nullptr)
588 {
589 json["BaseSpeedPriorityState"] =
590 *baseSpeedPriorityEnabled ? "Enabled" : "Disabled";
591 }
592 });
Jonathan Domandba0c292020-12-02 15:34:13 -0800593}
594
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600595/**
596 * @brief Fill out location info of a processor by
597 * requesting data from the given D-Bus object.
598 *
599 * @param[in,out] aResp Async HTTP response.
600 * @param[in] service D-Bus service to query.
601 * @param[in] objPath D-Bus object to query.
602 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800603inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> aResp,
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600604 const std::string& service,
605 const std::string& objPath)
606{
607 BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700608 sdbusplus::asio::getProperty<std::string>(
609 *crow::connections::systemBus, service, objPath,
610 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
611 [objPath, aResp{std::move(aResp)}](const boost::system::error_code ec,
612 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 if (ec)
614 {
615 BMCWEB_LOG_DEBUG << "DBUS response error";
616 messages::internalError(aResp->res);
617 return;
618 }
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600619
Ed Tanous002d39b2022-05-31 08:59:27 -0700620 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
621 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700622 });
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600623}
624
Jonathan Domanc9514482021-02-24 09:20:51 -0800625/**
Jonathan Doman49e429c2021-03-03 13:11:44 -0800626 * Populate the unique identifier in a Processor resource by requesting data
627 * from the given D-Bus object.
628 *
629 * @param[in,out] aResp Async HTTP response.
630 * @param[in] service D-Bus service to query.
631 * @param[in] objPath D-Bus object to query.
632 */
633inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
634 const std::string& service,
635 const std::string& objectPath)
636{
637 BMCWEB_LOG_DEBUG << "Get CPU UniqueIdentifier";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700638 sdbusplus::asio::getProperty<std::string>(
639 *crow::connections::systemBus, service, objectPath,
640 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
641 "UniqueIdentifier",
642 [aResp](boost::system::error_code ec, const std::string& id) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700643 if (ec)
644 {
645 BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
646 messages::internalError(aResp->res);
647 return;
648 }
649 aResp->res.jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] =
650 id;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700651 });
Jonathan Doman49e429c2021-03-03 13:11:44 -0800652}
653
654/**
Jonathan Domanc9514482021-02-24 09:20:51 -0800655 * Find the D-Bus object representing the requested Processor, and call the
656 * handler with the results. If matching object is not found, add 404 error to
657 * response and don't call the handler.
658 *
659 * @param[in,out] resp Async HTTP response.
660 * @param[in] processorId Redfish Processor Id.
661 * @param[in] handler Callback to continue processing request upon
662 * successfully finding object.
663 */
664template <typename Handler>
zhanghch058d1b46d2021-04-01 11:18:24 +0800665inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800666 const std::string& processorId,
667 Handler&& handler)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500668{
669 BMCWEB_LOG_DEBUG << "Get available system processor resources.";
670
Jonathan Domanc9514482021-02-24 09:20:51 -0800671 // GetSubTree on all interfaces which provide info about a Processor
Gunnar Millsac6a4442020-10-14 14:55:29 -0500672 crow::connections::systemBus->async_method_call(
Jonathan Domanc9514482021-02-24 09:20:51 -0800673 [resp, processorId, handler = std::forward<Handler>(handler)](
674 boost::system::error_code ec,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600675 const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700676 if (ec)
677 {
678 BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
679 messages::internalError(resp->res);
680 return;
681 }
682 for (const auto& [objectPath, serviceMap] : subtree)
683 {
684 // Ignore any objects which don't end with our desired cpu name
Ed Tanous11ba3972022-07-11 09:50:41 -0700685 if (!objectPath.ends_with(processorId))
Gunnar Millsac6a4442020-10-14 14:55:29 -0500686 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700687 continue;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500688 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700689
690 bool found = false;
691 // Filter out objects that don't have the CPU-specific
692 // interfaces to make sure we can return 404 on non-CPUs
693 // (e.g. /redfish/../Processors/dimm0)
694 for (const auto& [serviceName, interfaceList] : serviceMap)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500695 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700696 if (std::find_first_of(
697 interfaceList.begin(), interfaceList.end(),
698 processorInterfaces.begin(),
699 processorInterfaces.end()) != interfaceList.end())
Gunnar Millsac6a4442020-10-14 14:55:29 -0500700 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700701 found = true;
702 break;
Jonathan Doman2bab9832020-12-02 15:27:40 -0800703 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500704 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700705
706 if (!found)
707 {
708 continue;
709 }
710
711 // Process the first object which does match our cpu name and
712 // required interfaces, and potentially ignore any other
713 // matching objects. Assume all interfaces we want to process
714 // must be on the same object path.
715
Ed Tanous8a592812022-06-04 09:06:59 -0700716 handler(objectPath, serviceMap);
Ed Tanous002d39b2022-05-31 08:59:27 -0700717 return;
718 }
719 messages::resourceNotFound(resp->res, "Processor", processorId);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500720 },
721 "xyz.openbmc_project.ObjectMapper",
722 "/xyz/openbmc_project/object_mapper",
723 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
Jonathan Doman2bab9832020-12-02 15:27:40 -0800724 "/xyz/openbmc_project/inventory", 0,
Jonathan Doman49e429c2021-03-03 13:11:44 -0800725 std::array<const char*, 8>{
Sharad Yadav71b82f22021-05-10 15:11:39 +0530726 "xyz.openbmc_project.Common.UUID",
Jonathan Doman2bab9832020-12-02 15:27:40 -0800727 "xyz.openbmc_project.Inventory.Decorator.Asset",
728 "xyz.openbmc_project.Inventory.Decorator.Revision",
729 "xyz.openbmc_project.Inventory.Item.Cpu",
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600730 "xyz.openbmc_project.Inventory.Decorator.LocationCode",
Jonathan Domandba0c292020-12-02 15:34:13 -0800731 "xyz.openbmc_project.Inventory.Item.Accelerator",
Jonathan Doman49e429c2021-03-03 13:11:44 -0800732 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
733 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier"});
Gunnar Millsac6a4442020-10-14 14:55:29 -0500734}
735
zhanghch058d1b46d2021-04-01 11:18:24 +0800736inline void getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800737 const std::string& processorId,
738 const std::string& objectPath,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600739 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Domanc9514482021-02-24 09:20:51 -0800740{
741 for (const auto& [serviceName, interfaceList] : serviceMap)
742 {
743 for (const auto& interface : interfaceList)
744 {
745 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
746 {
747 getCpuAssetData(aResp, serviceName, objectPath);
748 }
George Liu0fda0f12021-11-16 10:06:17 +0800749 else if (interface ==
750 "xyz.openbmc_project.Inventory.Decorator.Revision")
Jonathan Domanc9514482021-02-24 09:20:51 -0800751 {
752 getCpuRevisionData(aResp, serviceName, objectPath);
753 }
754 else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
755 {
756 getCpuDataByService(aResp, processorId, serviceName,
757 objectPath);
758 }
George Liu0fda0f12021-11-16 10:06:17 +0800759 else if (interface ==
760 "xyz.openbmc_project.Inventory.Item.Accelerator")
Jonathan Domanc9514482021-02-24 09:20:51 -0800761 {
762 getAcceleratorDataByService(aResp, processorId, serviceName,
763 objectPath);
764 }
George Liu0fda0f12021-11-16 10:06:17 +0800765 else if (
766 interface ==
767 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig")
Jonathan Domanc9514482021-02-24 09:20:51 -0800768 {
769 getCpuConfigData(aResp, processorId, serviceName, objectPath);
770 }
George Liu0fda0f12021-11-16 10:06:17 +0800771 else if (interface ==
772 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
Jonathan Domanc9514482021-02-24 09:20:51 -0800773 {
774 getCpuLocationCode(aResp, serviceName, objectPath);
775 }
Sharad Yadav71b82f22021-05-10 15:11:39 +0530776 else if (interface == "xyz.openbmc_project.Common.UUID")
777 {
778 getProcessorUUID(aResp, serviceName, objectPath);
779 }
George Liu0fda0f12021-11-16 10:06:17 +0800780 else if (interface ==
781 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier")
Jonathan Doman49e429c2021-03-03 13:11:44 -0800782 {
783 getCpuUniqueId(aResp, serviceName, objectPath);
784 }
Jonathan Domanc9514482021-02-24 09:20:51 -0800785 }
786 }
787}
788
Jonathan Domandba0c292020-12-02 15:34:13 -0800789/**
790 * Request all the properties for the given D-Bus object and fill out the
791 * related entries in the Redfish OperatingConfig response.
792 *
793 * @param[in,out] aResp Async HTTP response.
794 * @param[in] service D-Bus service name to query.
795 * @param[in] objPath D-Bus object to query.
796 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800797inline void
798 getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
799 const std::string& service,
800 const std::string& objPath)
Jonathan Domandba0c292020-12-02 15:34:13 -0800801{
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200802 sdbusplus::asio::getAllProperties(
803 *crow::connections::systemBus, service, objPath,
804 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig",
Ed Tanous914e2d52022-01-07 11:38:34 -0800805 [aResp](const boost::system::error_code ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200806 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700807 if (ec)
808 {
809 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
810 messages::internalError(aResp->res);
811 return;
812 }
813
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200814 const size_t* availableCoreCount = nullptr;
815 const uint32_t* baseSpeed = nullptr;
816 const uint32_t* maxJunctionTemperature = nullptr;
817 const uint32_t* maxSpeed = nullptr;
818 const uint32_t* powerLimit = nullptr;
819 const TurboProfileProperty* turboProfile = nullptr;
820 const BaseSpeedPrioritySettingsProperty* baseSpeedPrioritySettings =
821 nullptr;
822
823 const bool success = sdbusplus::unpackPropertiesNoThrow(
824 dbus_utils::UnpackErrorPrinter(), properties, "AvailableCoreCount",
825 availableCoreCount, "BaseSpeed", baseSpeed,
826 "MaxJunctionTemperature", maxJunctionTemperature, "MaxSpeed",
827 maxSpeed, "PowerLimit", powerLimit, "TurboProfile", turboProfile,
828 "BaseSpeedPrioritySettings", baseSpeedPrioritySettings);
829
830 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700831 {
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200832 messages::internalError(aResp->res);
833 return;
834 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700835
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200836 nlohmann::json& json = aResp->res.jsonValue;
Ed Tanous002d39b2022-05-31 08:59:27 -0700837
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200838 if (availableCoreCount != nullptr)
839 {
840 json["TotalAvailableCoreCount"] = *availableCoreCount;
841 }
842
843 if (baseSpeed != nullptr)
844 {
845 json["BaseSpeedMHz"] = *baseSpeed;
846 }
847
848 if (maxJunctionTemperature != nullptr)
849 {
850 json["MaxJunctionTemperatureCelsius"] = *maxJunctionTemperature;
851 }
852
853 if (maxSpeed != nullptr)
854 {
855 json["MaxSpeedMHz"] = *maxSpeed;
856 }
857
858 if (powerLimit != nullptr)
859 {
860 json["TDPWatts"] = *powerLimit;
861 }
862
863 if (turboProfile != nullptr)
864 {
865 nlohmann::json& turboArray = json["TurboProfile"];
866 turboArray = nlohmann::json::array();
867 for (const auto& [turboSpeed, coreCount] : *turboProfile)
868 {
869 nlohmann::json::object_t turbo;
870 turbo["ActiveCoreCount"] = coreCount;
871 turbo["MaxSpeedMHz"] = turboSpeed;
872 turboArray.push_back(std::move(turbo));
Ed Tanous002d39b2022-05-31 08:59:27 -0700873 }
874 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200875
876 if (baseSpeedPrioritySettings != nullptr)
877 {
878 nlohmann::json& baseSpeedArray = json["BaseSpeedPrioritySettings"];
879 baseSpeedArray = nlohmann::json::array();
880 for (const auto& [baseSpeedMhz, coreList] :
881 *baseSpeedPrioritySettings)
882 {
883 nlohmann::json::object_t speed;
884 speed["CoreCount"] = coreList.size();
885 speed["CoreIDs"] = coreList;
886 speed["BaseSpeedMHz"] = baseSpeedMhz;
887 baseSpeedArray.push_back(std::move(speed));
888 }
889 }
890 });
Jonathan Domandba0c292020-12-02 15:34:13 -0800891}
892
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800893/**
894 * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
895 * property. Main task is to translate error messages into Redfish errors.
896 *
897 * @param[in,out] resp HTTP response.
898 * @param[in] setPropVal Value which we attempted to set.
899 * @param[in] ec D-Bus response error code.
900 * @param[in] msg D-Bus response message.
901 */
902inline void
903 handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
904 const std::string& setPropVal,
905 boost::system::error_code ec,
Patrick Williams59d494e2022-07-22 19:26:55 -0500906 const sdbusplus::message_t& msg)
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800907{
908 if (!ec)
909 {
910 BMCWEB_LOG_DEBUG << "Set Property succeeded";
911 return;
912 }
913
914 BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
915
916 const sd_bus_error* dbusError = msg.get_error();
917 if (dbusError == nullptr)
918 {
919 messages::internalError(resp->res);
920 return;
921 }
922
923 // The asio error code doesn't know about our custom errors, so we have to
924 // parse the error string. Some of these D-Bus -> Redfish translations are a
925 // stretch, but it's good to try to communicate something vaguely useful.
926 if (strcmp(dbusError->name,
927 "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
928 {
929 // Service did not like the object_path we tried to set.
930 messages::propertyValueIncorrect(
931 resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
932 }
933 else if (strcmp(dbusError->name,
934 "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
935 {
936 // Service indicates we can never change the config for this processor.
937 messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
938 }
939 else if (strcmp(dbusError->name,
940 "xyz.openbmc_project.Common.Error.Unavailable") == 0)
941 {
942 // Service indicates the config cannot be changed right now, but maybe
943 // in a different system state.
944 messages::resourceInStandby(resp->res);
945 }
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800946 else
947 {
948 messages::internalError(resp->res);
949 }
950}
951
952/**
953 * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
954 * validation of the input data, and then set the D-Bus property.
955 *
956 * @param[in,out] resp Async HTTP response.
957 * @param[in] processorId Processor's Id.
958 * @param[in] appliedConfigUri New property value to apply.
959 * @param[in] cpuObjectPath Path of CPU object to modify.
960 * @param[in] serviceMap Service map for CPU object.
961 */
962inline void patchAppliedOperatingConfig(
963 const std::shared_ptr<bmcweb::AsyncResp>& resp,
964 const std::string& processorId, const std::string& appliedConfigUri,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600965 const std::string& cpuObjectPath,
966 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800967{
968 // Check that the property even exists by checking for the interface
969 const std::string* controlService = nullptr;
970 for (const auto& [serviceName, interfaceList] : serviceMap)
971 {
972 if (std::find(interfaceList.begin(), interfaceList.end(),
973 "xyz.openbmc_project.Control.Processor."
974 "CurrentOperatingConfig") != interfaceList.end())
975 {
976 controlService = &serviceName;
977 break;
978 }
979 }
980
981 if (controlService == nullptr)
982 {
983 messages::internalError(resp->res);
984 return;
985 }
986
987 // Check that the config URI is a child of the cpu URI being patched.
988 std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
989 expectedPrefix += processorId;
990 expectedPrefix += "/OperatingConfigs/";
Ed Tanous11ba3972022-07-11 09:50:41 -0700991 if (!appliedConfigUri.starts_with(expectedPrefix) ||
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800992 expectedPrefix.size() == appliedConfigUri.size())
993 {
994 messages::propertyValueIncorrect(
995 resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
996 return;
997 }
998
999 // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
1000 // direct child of the CPU object.
1001 // Strip the expectedPrefix from the config URI to get the "filename", and
1002 // append to the CPU's path.
1003 std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
1004 sdbusplus::message::object_path configPath(cpuObjectPath);
1005 configPath /= configBaseName;
1006
1007 BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
1008
1009 // Set the property, with handler to check error responses
1010 crow::connections::systemBus->async_method_call(
Ed Tanous914e2d52022-01-07 11:38:34 -08001011 [resp, appliedConfigUri](const boost::system::error_code ec,
Patrick Williams59d494e2022-07-22 19:26:55 -05001012 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001013 handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001014 },
1015 *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
1016 "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Ed Tanous168e20c2021-12-13 14:39:53 -08001017 "AppliedConfig", dbus::utility::DbusVariantType(std::move(configPath)));
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001018}
1019
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001020inline void requestRoutesOperatingConfigCollection(App& app)
Jonathan Domandba0c292020-12-02 15:34:13 -08001021{
Jonathan Domandba0c292020-12-02 15:34:13 -08001022
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001023 BMCWEB_ROUTE(
1024 app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
Ed Tanoused398212021-06-09 17:05:54 -07001025 .privileges(redfish::privileges::getOperatingConfigCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001026 .methods(boost::beast::http::verb::get)(
1027 [&app](const crow::Request& req,
1028 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1029 const std::string& cpuName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001030 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001031 {
1032 return;
1033 }
1034 asyncResp->res.jsonValue["@odata.type"] =
1035 "#OperatingConfigCollection.OperatingConfigCollection";
1036 asyncResp->res.jsonValue["@odata.id"] = req.url;
1037 asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
1038
1039 // First find the matching CPU object so we know how to
1040 // constrain our search for related Config objects.
1041 crow::connections::systemBus->async_method_call(
1042 [asyncResp, cpuName](
1043 const boost::system::error_code ec,
1044 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
1045 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001046 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001047 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1048 << ec.message();
1049 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001050 return;
1051 }
Jonathan Domandba0c292020-12-02 15:34:13 -08001052
Ed Tanous002d39b2022-05-31 08:59:27 -07001053 for (const std::string& object : objects)
1054 {
Ed Tanous11ba3972022-07-11 09:50:41 -07001055 if (!object.ends_with(cpuName))
Ed Tanous002d39b2022-05-31 08:59:27 -07001056 {
1057 continue;
1058 }
George Liu0fda0f12021-11-16 10:06:17 +08001059
Ed Tanous002d39b2022-05-31 08:59:27 -07001060 // Not expected that there will be multiple matching
1061 // CPU objects, but if there are just use the first
1062 // one.
Jonathan Domandba0c292020-12-02 15:34:13 -08001063
Ed Tanous002d39b2022-05-31 08:59:27 -07001064 // Use the common search routine to construct the
1065 // Collection of all Config objects under this CPU.
1066 collection_util::getCollectionMembers(
1067 asyncResp,
1068 "/redfish/v1/Systems/system/Processors/" + cpuName +
1069 "/OperatingConfigs",
1070 {"xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"},
1071 object.c_str());
1072 return;
1073 }
1074 },
1075 "xyz.openbmc_project.ObjectMapper",
1076 "/xyz/openbmc_project/object_mapper",
1077 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
1078 "/xyz/openbmc_project/inventory", 0,
1079 std::array<const char*, 1>{
1080 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"});
George Liu0fda0f12021-11-16 10:06:17 +08001081 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001082}
1083
1084inline void requestRoutesOperatingConfig(App& app)
1085{
1086 BMCWEB_ROUTE(
1087 app,
1088 "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001089 .privileges(redfish::privileges::getOperatingConfig)
Ed Tanous002d39b2022-05-31 08:59:27 -07001090 .methods(boost::beast::http::verb::get)(
1091 [&app](const crow::Request& req,
1092 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1093 const std::string& cpuName, const std::string& configName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001094 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 {
1096 return;
1097 }
1098 // Ask for all objects implementing OperatingConfig so we can search
1099 // for one with a matching name
1100 crow::connections::systemBus->async_method_call(
1101 [asyncResp, cpuName, configName, reqUrl{req.url}](
1102 boost::system::error_code ec,
1103 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1104 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001105 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001106 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1107 << ec.message();
1108 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001109 return;
1110 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001111 const std::string expectedEnding = cpuName + '/' + configName;
1112 for (const auto& [objectPath, serviceMap] : subtree)
1113 {
1114 // Ignore any configs without matching cpuX/configY
Ed Tanous11ba3972022-07-11 09:50:41 -07001115 if (!objectPath.ends_with(expectedEnding) || serviceMap.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -07001116 {
1117 continue;
1118 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001119
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 nlohmann::json& json = asyncResp->res.jsonValue;
1121 json["@odata.type"] = "#OperatingConfig.v1_0_0.OperatingConfig";
1122 json["@odata.id"] = reqUrl;
1123 json["Name"] = "Processor Profile";
1124 json["Id"] = configName;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001125
Ed Tanous002d39b2022-05-31 08:59:27 -07001126 // Just use the first implementation of the object - not
1127 // expected that there would be multiple matching
1128 // services
1129 getOperatingConfigData(asyncResp, serviceMap.begin()->first,
1130 objectPath);
1131 return;
1132 }
1133 messages::resourceNotFound(asyncResp->res, "OperatingConfig",
1134 configName);
1135 },
1136 "xyz.openbmc_project.ObjectMapper",
1137 "/xyz/openbmc_project/object_mapper",
1138 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1139 "/xyz/openbmc_project/inventory", 0,
1140 std::array<const char*, 1>{
1141 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001142 });
1143}
Jonathan Domandba0c292020-12-02 15:34:13 -08001144
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001145inline void requestRoutesProcessorCollection(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001146{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001147 /**
1148 * Functions triggers appropriate requests on DBus
1149 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001150 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/")
Ed Tanoused398212021-06-09 17:05:54 -07001151 .privileges(redfish::privileges::getProcessorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001152 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001153 [&app](const crow::Request& req,
1154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001155 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001156 {
1157 return;
1158 }
1159 asyncResp->res.jsonValue["@odata.type"] =
1160 "#ProcessorCollection.ProcessorCollection";
1161 asyncResp->res.jsonValue["Name"] = "Processor Collection";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001162
Ed Tanous002d39b2022-05-31 08:59:27 -07001163 asyncResp->res.jsonValue["@odata.id"] =
1164 "/redfish/v1/Systems/system/Processors";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001165
Ed Tanous002d39b2022-05-31 08:59:27 -07001166 collection_util::getCollectionMembers(
1167 asyncResp, "/redfish/v1/Systems/system/Processors",
1168 std::vector<const char*>(processorInterfaces.begin(),
1169 processorInterfaces.end()));
1170 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001171}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001172
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001173inline void requestRoutesProcessor(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001174{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001175 /**
1176 * Functions triggers appropriate requests on DBus
1177 */
Gunnar Millsac6a4442020-10-14 14:55:29 -05001178
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001179 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001180 .privileges(redfish::privileges::getProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001181 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001182 [&app](const crow::Request& req,
1183 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1184 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001185 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 {
1187 return;
1188 }
1189 asyncResp->res.jsonValue["@odata.type"] =
1190 "#Processor.v1_11_0.Processor";
1191 asyncResp->res.jsonValue["@odata.id"] =
1192 "/redfish/v1/Systems/system/Processors/" + processorId;
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001193
Ed Tanous8a592812022-06-04 09:06:59 -07001194 getProcessorObject(
1195 asyncResp, processorId,
1196 std::bind_front(getProcessorData, asyncResp, processorId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001197 });
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001198
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001199 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001200 .privileges(redfish::privileges::patchProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001201 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001202 [&app](const crow::Request& req,
1203 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1204 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001205 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 {
1207 return;
1208 }
1209 std::optional<nlohmann::json> appliedConfigJson;
1210 if (!json_util::readJsonPatch(req, asyncResp->res,
1211 "AppliedOperatingConfig",
1212 appliedConfigJson))
1213 {
1214 return;
1215 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001216
Ed Tanous002d39b2022-05-31 08:59:27 -07001217 std::string appliedConfigUri;
1218 if (appliedConfigJson)
1219 {
1220 if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
1221 "@odata.id", appliedConfigUri))
1222 {
1223 return;
1224 }
1225 // Check for 404 and find matching D-Bus object, then run
1226 // property patch handlers if that all succeeds.
Ed Tanous8a592812022-06-04 09:06:59 -07001227 getProcessorObject(asyncResp, processorId,
1228 std::bind_front(patchAppliedOperatingConfig,
1229 asyncResp, processorId,
1230 appliedConfigUri));
Ed Tanous002d39b2022-05-31 08:59:27 -07001231 }
1232 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001233}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001234
1235} // namespace redfish