blob: 873f8824621182cf631478118c8fa6f8ed4ca7f1 [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"
Chris Caindfbf7de2023-04-13 16:01:04 -050022#include "generated/enums/processor.hpp"
Gunnar Millsac6a4442020-10-14 14:55:29 -050023#include "health.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "query.hpp"
25#include "registries/privilege_registry.hpp"
26#include "utils/collection.hpp"
27#include "utils/dbus_utils.hpp"
28#include "utils/json_utils.hpp"
Gunnar Millsac6a4442020-10-14 14:55:29 -050029
30#include <boost/container/flat_map.hpp>
George Liue99073f2022-12-09 11:06:16 +080031#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070032#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070033#include <sdbusplus/asio/property.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080034#include <sdbusplus/message/native_types.hpp>
Krzysztof Grobelny351053f2022-07-28 15:44:22 +020035#include <sdbusplus/unpack_properties.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080036#include <sdbusplus/utility/dedup_variant.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050037
George Liu7a1dbc42022-12-07 16:03:22 +080038#include <array>
Michael Shenb9d679d2023-02-13 02:29:04 +000039#include <limits>
George Liu7a1dbc42022-12-07 16:03:22 +080040#include <string_view>
41
Gunnar Millsac6a4442020-10-14 14:55:29 -050042namespace redfish
43{
44
Jonathan Domanc9514482021-02-24 09:20:51 -080045// Interfaces which imply a D-Bus object represents a Processor
George Liu7a1dbc42022-12-07 16:03:22 +080046constexpr std::array<std::string_view, 2> processorInterfaces = {
Jonathan Domanc9514482021-02-24 09:20:51 -080047 "xyz.openbmc_project.Inventory.Item.Cpu",
48 "xyz.openbmc_project.Inventory.Item.Accelerator"};
Jonathan Doman2bab9832020-12-02 15:27:40 -080049
Sharad Yadav71b82f22021-05-10 15:11:39 +053050/**
51 * @brief Fill out uuid info of a processor by
52 * requesting data from the given D-Bus object.
53 *
Ed Tanousac106bf2023-06-07 09:24:59 -070054 * @param[in,out] asyncResp Async HTTP response.
Sharad Yadav71b82f22021-05-10 15:11:39 +053055 * @param[in] service D-Bus service to query.
56 * @param[in] objPath D-Bus object to query.
57 */
Ed Tanousac106bf2023-06-07 09:24:59 -070058inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Sharad Yadav71b82f22021-05-10 15:11:39 +053059 const std::string& service,
60 const std::string& objPath)
61{
Ed Tanous62598e32023-07-17 17:06:25 -070062 BMCWEB_LOG_DEBUG("Get Processor UUID");
Jonathan Doman1e1e5982021-06-11 09:36:17 -070063 sdbusplus::asio::getProperty<std::string>(
64 *crow::connections::systemBus, service, objPath,
65 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanousac106bf2023-06-07 09:24:59 -070066 [objPath, asyncResp{std::move(asyncResp)}](
67 const boost::system::error_code& ec, const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -070068 if (ec)
69 {
Ed Tanous62598e32023-07-17 17:06:25 -070070 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -070071 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -070072 return;
73 }
Ed Tanousac106bf2023-06-07 09:24:59 -070074 asyncResp->res.jsonValue["UUID"] = property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -070075 });
Sharad Yadav71b82f22021-05-10 15:11:39 +053076}
77
Ed Tanous711ac7a2021-12-20 09:34:41 -080078inline void getCpuDataByInterface(
Ed Tanousac106bf2023-06-07 09:24:59 -070079 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -080080 const dbus::utility::DBusInteracesMap& cpuInterfacesProperties)
Gunnar Millsac6a4442020-10-14 14:55:29 -050081{
Ed Tanous62598e32023-07-17 17:06:25 -070082 BMCWEB_LOG_DEBUG("Get CPU resources by interface.");
Gunnar Millsac6a4442020-10-14 14:55:29 -050083
Chicago Duana1649ec2021-03-30 16:54:58 +080084 // Set the default value of state
Ed Tanousac106bf2023-06-07 09:24:59 -070085 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
86 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -050087
88 for (const auto& interface : cpuInterfacesProperties)
89 {
90 for (const auto& property : interface.second)
91 {
Chicago Duana1649ec2021-03-30 16:54:58 +080092 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -050093 {
Chicago Duana1649ec2021-03-30 16:54:58 +080094 const bool* cpuPresent = std::get_if<bool>(&property.second);
95 if (cpuPresent == nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -050096 {
97 // Important property not in desired type
Ed Tanousac106bf2023-06-07 09:24:59 -070098 messages::internalError(asyncResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -050099 return;
100 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800101 if (!*cpuPresent)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500102 {
Chicago Duana1649ec2021-03-30 16:54:58 +0800103 // Slot is not populated
Ed Tanousac106bf2023-06-07 09:24:59 -0700104 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
Chicago Duana1649ec2021-03-30 16:54:58 +0800105 }
106 }
107 else if (property.first == "Functional")
108 {
109 const bool* cpuFunctional = std::get_if<bool>(&property.second);
110 if (cpuFunctional == nullptr)
111 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700112 messages::internalError(asyncResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500113 return;
114 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800115 if (!*cpuFunctional)
Chicago Duana1649ec2021-03-30 16:54:58 +0800116 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700117 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
Chicago Duana1649ec2021-03-30 16:54:58 +0800118 }
119 }
120 else if (property.first == "CoreCount")
121 {
122 const uint16_t* coresCount =
123 std::get_if<uint16_t>(&property.second);
124 if (coresCount == nullptr)
125 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700126 messages::internalError(asyncResp->res);
Chicago Duana1649ec2021-03-30 16:54:58 +0800127 return;
128 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700129 asyncResp->res.jsonValue["TotalCores"] = *coresCount;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500130 }
Jonathan Domandc3fa662020-10-26 23:10:24 -0700131 else if (property.first == "MaxSpeedInMhz")
132 {
133 const uint32_t* value = std::get_if<uint32_t>(&property.second);
134 if (value != nullptr)
135 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700136 asyncResp->res.jsonValue["MaxSpeedMHz"] = *value;
Jonathan Domandc3fa662020-10-26 23:10:24 -0700137 }
138 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500139 else if (property.first == "Socket")
140 {
141 const std::string* value =
142 std::get_if<std::string>(&property.second);
143 if (value != nullptr)
144 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700145 asyncResp->res.jsonValue["Socket"] = *value;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500146 }
147 }
148 else if (property.first == "ThreadCount")
149 {
Jonathan Domandc3fa662020-10-26 23:10:24 -0700150 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500151 if (value != nullptr)
152 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700153 asyncResp->res.jsonValue["TotalThreads"] = *value;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500154 }
155 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700156 else if (property.first == "EffectiveFamily")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500157 {
Brandon Kim1930fbd2021-09-14 17:52:51 -0700158 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Brad Bishop6169de22022-09-14 13:08:32 -0400159 if (value != nullptr && *value != 2)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500160 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700161 asyncResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800162 "0x" + intToHexString(*value, 4);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500163 }
164 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700165 else if (property.first == "EffectiveModel")
166 {
167 const uint16_t* value = std::get_if<uint16_t>(&property.second);
168 if (value == nullptr)
169 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700170 messages::internalError(asyncResp->res);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700171 return;
172 }
Brad Bishop6169de22022-09-14 13:08:32 -0400173 if (*value != 0)
174 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700175 asyncResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
Brad Bishop6169de22022-09-14 13:08:32 -0400176 "0x" + intToHexString(*value, 4);
177 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700178 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500179 else if (property.first == "Id")
180 {
181 const uint64_t* value = std::get_if<uint64_t>(&property.second);
182 if (value != nullptr && *value != 0)
183 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700184 asyncResp->res
Gunnar Millsac6a4442020-10-14 14:55:29 -0500185 .jsonValue["ProcessorId"]["IdentificationRegisters"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800186 "0x" + intToHexString(*value, 16);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500187 }
188 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700189 else if (property.first == "Microcode")
190 {
191 const uint32_t* value = std::get_if<uint32_t>(&property.second);
192 if (value == nullptr)
193 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700194 messages::internalError(asyncResp->res);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700195 return;
196 }
Brad Bishop6169de22022-09-14 13:08:32 -0400197 if (*value != 0)
198 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700199 asyncResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
Brad Bishop6169de22022-09-14 13:08:32 -0400200 "0x" + intToHexString(*value, 8);
201 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700202 }
203 else if (property.first == "Step")
204 {
205 const uint16_t* value = std::get_if<uint16_t>(&property.second);
206 if (value == nullptr)
207 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700208 messages::internalError(asyncResp->res);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700209 return;
210 }
Michael Shenb9d679d2023-02-13 02:29:04 +0000211 if (*value != std::numeric_limits<uint16_t>::max())
Brad Bishop6169de22022-09-14 13:08:32 -0400212 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700213 asyncResp->res.jsonValue["ProcessorId"]["Step"] =
Brad Bishop6169de22022-09-14 13:08:32 -0400214 "0x" + intToHexString(*value, 4);
215 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700216 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500217 }
218 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500219}
220
Ed Tanousac106bf2023-06-07 09:24:59 -0700221inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500222 const std::string& cpuId,
223 const std::string& service,
224 const std::string& objPath)
225{
Ed Tanous62598e32023-07-17 17:06:25 -0700226 BMCWEB_LOG_DEBUG("Get available system cpu resources by service.");
Gunnar Millsac6a4442020-10-14 14:55:29 -0500227
George Liu5eb468d2023-06-20 17:03:24 +0800228 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
229 dbus::utility::getManagedObjects(
230 service, path,
Ed Tanousac106bf2023-06-07 09:24:59 -0700231 [cpuId, service, objPath, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800232 const boost::system::error_code& ec,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500233 const dbus::utility::ManagedObjectType& dbusData) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700234 if (ec)
235 {
Ed Tanous62598e32023-07-17 17:06:25 -0700236 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700237 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700238 return;
239 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700240 asyncResp->res.jsonValue["Id"] = cpuId;
241 asyncResp->res.jsonValue["Name"] = "Processor";
242 asyncResp->res.jsonValue["ProcessorType"] = "CPU";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500243
Ed Tanous002d39b2022-05-31 08:59:27 -0700244 bool slotPresent = false;
245 std::string corePath = objPath + "/core";
246 size_t totalCores = 0;
247 for (const auto& object : dbusData)
248 {
249 if (object.first.str == objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500250 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700251 getCpuDataByInterface(asyncResp, object.second);
Ed Tanous002d39b2022-05-31 08:59:27 -0700252 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700253 else if (object.first.str.starts_with(corePath))
Ed Tanous002d39b2022-05-31 08:59:27 -0700254 {
255 for (const auto& interface : object.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500256 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700257 if (interface.first == "xyz.openbmc_project.Inventory.Item")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500258 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700259 for (const auto& property : interface.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500260 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700261 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500262 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700263 const bool* present =
264 std::get_if<bool>(&property.second);
265 if (present != nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500266 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700267 if (*present)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500268 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700269 slotPresent = true;
270 totalCores++;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500271 }
272 }
273 }
274 }
275 }
276 }
277 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700278 }
279 // In getCpuDataByInterface(), state and health are set
280 // based on the present and functional status. If core
281 // count is zero, then it has a higher precedence.
282 if (slotPresent)
283 {
284 if (totalCores == 0)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500285 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700286 // Slot is not populated, set status end return
Ed Tanousac106bf2023-06-07 09:24:59 -0700287 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
288 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500289 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700290 asyncResp->res.jsonValue["TotalCores"] = totalCores;
Ed Tanous002d39b2022-05-31 08:59:27 -0700291 }
292 return;
George Liu5eb468d2023-06-20 17:03:24 +0800293 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500294}
295
Chris Caindfbf7de2023-04-13 16:01:04 -0500296/**
297 * @brief Translates throttle cause DBUS property to redfish.
298 *
299 * @param[in] dbusSource The throttle cause from DBUS
300 *
301 * @return Returns as a string, the throttle cause in Redfish terms. If
302 * translation cannot be done, returns "Unknown" throttle reason.
303 */
304inline processor::ThrottleCause
305 dbusToRfThrottleCause(const std::string& dbusSource)
306{
307 if (dbusSource ==
308 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.ClockLimit")
309 {
310 return processor::ThrottleCause::ClockLimit;
311 }
312 if (dbusSource ==
313 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.ManagementDetectedFault")
314 {
315 return processor::ThrottleCause::ManagementDetectedFault;
316 }
317 if (dbusSource ==
318 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.PowerLimit")
319 {
320 return processor::ThrottleCause::PowerLimit;
321 }
322 if (dbusSource ==
323 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.ThermalLimit")
324 {
325 return processor::ThrottleCause::ThermalLimit;
326 }
327 if (dbusSource ==
328 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.Unknown")
329 {
330 return processor::ThrottleCause::Unknown;
331 }
332 return processor::ThrottleCause::Invalid;
333}
334
335inline void
Ed Tanousac106bf2023-06-07 09:24:59 -0700336 readThrottleProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Chris Caindfbf7de2023-04-13 16:01:04 -0500337 const boost::system::error_code& ec,
338 const dbus::utility::DBusPropertiesMap& properties)
339{
340 if (ec)
341 {
Ed Tanous62598e32023-07-17 17:06:25 -0700342 BMCWEB_LOG_ERROR("Processor Throttle getAllProperties error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -0700343 messages::internalError(asyncResp->res);
Chris Caindfbf7de2023-04-13 16:01:04 -0500344 return;
345 }
346
347 const bool* status = nullptr;
348 const std::vector<std::string>* causes = nullptr;
349
350 if (!sdbusplus::unpackPropertiesNoThrow(dbus_utils::UnpackErrorPrinter(),
351 properties, "Throttled", status,
352 "ThrottleCauses", causes))
353 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700354 messages::internalError(asyncResp->res);
Chris Caindfbf7de2023-04-13 16:01:04 -0500355 return;
356 }
357
Ed Tanousac106bf2023-06-07 09:24:59 -0700358 asyncResp->res.jsonValue["Throttled"] = *status;
Chris Caindfbf7de2023-04-13 16:01:04 -0500359 nlohmann::json::array_t rCauses;
360 for (const std::string& cause : *causes)
361 {
362 processor::ThrottleCause rfCause = dbusToRfThrottleCause(cause);
363 if (rfCause == processor::ThrottleCause::Invalid)
364 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700365 messages::internalError(asyncResp->res);
Chris Caindfbf7de2023-04-13 16:01:04 -0500366 return;
367 }
368
369 rCauses.emplace_back(rfCause);
370 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700371 asyncResp->res.jsonValue["ThrottleCauses"] = std::move(rCauses);
Chris Caindfbf7de2023-04-13 16:01:04 -0500372}
373
374inline void
Ed Tanousac106bf2023-06-07 09:24:59 -0700375 getThrottleProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Chris Caindfbf7de2023-04-13 16:01:04 -0500376 const std::string& service,
377 const std::string& objectPath)
378{
Ed Tanous62598e32023-07-17 17:06:25 -0700379 BMCWEB_LOG_DEBUG("Get processor throttle resources");
Chris Caindfbf7de2023-04-13 16:01:04 -0500380
381 sdbusplus::asio::getAllProperties(
382 *crow::connections::systemBus, service, objectPath,
383 "xyz.openbmc_project.Control.Power.Throttle",
Ed Tanousac106bf2023-06-07 09:24:59 -0700384 [asyncResp](const boost::system::error_code& ec,
385 const dbus::utility::DBusPropertiesMap& properties) {
386 readThrottleProperties(asyncResp, ec, properties);
Chris Caindfbf7de2023-04-13 16:01:04 -0500387 });
388}
389
Ed Tanousac106bf2023-06-07 09:24:59 -0700390inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500391 const std::string& service,
392 const std::string& objPath)
393{
Ed Tanous62598e32023-07-17 17:06:25 -0700394 BMCWEB_LOG_DEBUG("Get Cpu Asset Data");
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200395 sdbusplus::asio::getAllProperties(
396 *crow::connections::systemBus, service, objPath,
397 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanousac106bf2023-06-07 09:24:59 -0700398 [objPath, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800399 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 {
Ed Tanous62598e32023-07-17 17:06:25 -0700403 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700404 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700405 return;
406 }
407
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200408 const std::string* serialNumber = nullptr;
409 const std::string* model = nullptr;
410 const std::string* manufacturer = nullptr;
411 const std::string* partNumber = nullptr;
412 const std::string* sparePartNumber = nullptr;
413
414 const bool success = sdbusplus::unpackPropertiesNoThrow(
415 dbus_utils::UnpackErrorPrinter(), properties, "SerialNumber",
416 serialNumber, "Model", model, "Manufacturer", manufacturer,
417 "PartNumber", partNumber, "SparePartNumber", sparePartNumber);
418
419 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700420 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700421 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200422 return;
423 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700424
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200425 if (serialNumber != nullptr && !serialNumber->empty())
426 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700427 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200428 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700429
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200430 if ((model != nullptr) && !model->empty())
431 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700432 asyncResp->res.jsonValue["Model"] = *model;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200433 }
434
435 if (manufacturer != nullptr)
436 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700437 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200438
439 // Otherwise would be unexpected.
440 if (manufacturer->find("Intel") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700441 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700442 asyncResp->res.jsonValue["ProcessorArchitecture"] = "x86";
443 asyncResp->res.jsonValue["InstructionSet"] = "x86-64";
Ed Tanous002d39b2022-05-31 08:59:27 -0700444 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200445 else if (manufacturer->find("IBM") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700446 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700447 asyncResp->res.jsonValue["ProcessorArchitecture"] = "Power";
448 asyncResp->res.jsonValue["InstructionSet"] = "PowerISA";
Ed Tanous002d39b2022-05-31 08:59:27 -0700449 }
450 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200451
452 if (partNumber != nullptr)
453 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700454 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200455 }
456
Brad Bishop6169de22022-09-14 13:08:32 -0400457 if (sparePartNumber != nullptr && !sparePartNumber->empty())
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200458 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700459 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200460 }
461 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500462}
463
Ed Tanousac106bf2023-06-07 09:24:59 -0700464inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500465 const std::string& service,
466 const std::string& objPath)
467{
Ed Tanous62598e32023-07-17 17:06:25 -0700468 BMCWEB_LOG_DEBUG("Get Cpu Revision Data");
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200469 sdbusplus::asio::getAllProperties(
470 *crow::connections::systemBus, service, objPath,
471 "xyz.openbmc_project.Inventory.Decorator.Revision",
Ed Tanousac106bf2023-06-07 09:24:59 -0700472 [objPath, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800473 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200474 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 if (ec)
476 {
Ed Tanous62598e32023-07-17 17:06:25 -0700477 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700478 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700479 return;
480 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500481
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200482 const std::string* version = nullptr;
483
484 const bool success = sdbusplus::unpackPropertiesNoThrow(
485 dbus_utils::UnpackErrorPrinter(), properties, "Version", version);
486
487 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700488 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700489 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200490 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700491 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200492
493 if (version != nullptr)
494 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700495 asyncResp->res.jsonValue["Version"] = *version;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200496 }
497 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500498}
499
zhanghch058d1b46d2021-04-01 11:18:24 +0800500inline void getAcceleratorDataByService(
Ed Tanousac106bf2023-06-07 09:24:59 -0700501 std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& acclrtrId,
zhanghch058d1b46d2021-04-01 11:18:24 +0800502 const std::string& service, const std::string& objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500503{
Ed Tanous62598e32023-07-17 17:06:25 -0700504 BMCWEB_LOG_DEBUG("Get available system Accelerator resources by service.");
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200505 sdbusplus::asio::getAllProperties(
506 *crow::connections::systemBus, service, objPath, "",
Ed Tanousac106bf2023-06-07 09:24:59 -0700507 [acclrtrId, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800508 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200509 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700510 if (ec)
511 {
Ed Tanous62598e32023-07-17 17:06:25 -0700512 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700513 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700514 return;
515 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700516
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200517 const bool* functional = nullptr;
518 const bool* present = nullptr;
519
520 const bool success = sdbusplus::unpackPropertiesNoThrow(
521 dbus_utils::UnpackErrorPrinter(), properties, "Functional",
522 functional, "Present", present);
523
524 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700526 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200527 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700528 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500529
Ed Tanous002d39b2022-05-31 08:59:27 -0700530 std::string state = "Enabled";
531 std::string health = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500532
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200533 if (present != nullptr && !*present)
Ed Tanous002d39b2022-05-31 08:59:27 -0700534 {
535 state = "Absent";
536 }
537
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200538 if (functional != nullptr && !*functional)
Ed Tanous002d39b2022-05-31 08:59:27 -0700539 {
540 if (state == "Enabled")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500541 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 health = "Critical";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500543 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700544 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500545
Ed Tanousac106bf2023-06-07 09:24:59 -0700546 asyncResp->res.jsonValue["Id"] = acclrtrId;
547 asyncResp->res.jsonValue["Name"] = "Processor";
548 asyncResp->res.jsonValue["Status"]["State"] = state;
549 asyncResp->res.jsonValue["Status"]["Health"] = health;
550 asyncResp->res.jsonValue["ProcessorType"] = "Accelerator";
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200551 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500552}
553
Jonathan Domandba0c292020-12-02 15:34:13 -0800554// OperatingConfig D-Bus Types
555using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
556using BaseSpeedPrioritySettingsProperty =
557 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
558// uint32_t and size_t may or may not be the same type, requiring a dedup'd
559// variant
Jonathan Domandba0c292020-12-02 15:34:13 -0800560
561/**
562 * Fill out the HighSpeedCoreIDs in a Processor resource from the given
563 * OperatingConfig D-Bus property.
564 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700565 * @param[in,out] asyncResp Async HTTP response.
Jonathan Domandba0c292020-12-02 15:34:13 -0800566 * @param[in] baseSpeedSettings Full list of base speed priority groups,
567 * to use to determine the list of high
568 * speed cores.
569 */
570inline void highSpeedCoreIdsHandler(
Ed Tanousac106bf2023-06-07 09:24:59 -0700571 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800572 const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
573{
574 // The D-Bus property does not indicate which bucket is the "high
575 // priority" group, so let's discern that by looking for the one with
576 // highest base frequency.
577 auto highPriorityGroup = baseSpeedSettings.cend();
578 uint32_t highestBaseSpeed = 0;
579 for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
580 ++it)
581 {
582 const uint32_t baseFreq = std::get<uint32_t>(*it);
583 if (baseFreq > highestBaseSpeed)
584 {
585 highestBaseSpeed = baseFreq;
586 highPriorityGroup = it;
587 }
588 }
589
Ed Tanousac106bf2023-06-07 09:24:59 -0700590 nlohmann::json& jsonCoreIds = asyncResp->res.jsonValue["HighSpeedCoreIDs"];
Jonathan Domandba0c292020-12-02 15:34:13 -0800591 jsonCoreIds = nlohmann::json::array();
592
593 // There may not be any entries in the D-Bus property, so only populate
594 // if there was actually something there.
595 if (highPriorityGroup != baseSpeedSettings.cend())
596 {
597 jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
598 }
599}
600
601/**
602 * Fill out OperatingConfig related items in a Processor resource by requesting
603 * data from the given D-Bus object.
604 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700605 * @param[in,out] asyncResp Async HTTP response.
Jonathan Domandba0c292020-12-02 15:34:13 -0800606 * @param[in] cpuId CPU D-Bus name.
607 * @param[in] service D-Bus service to query.
608 * @param[in] objPath D-Bus object to query.
609 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700610inline void
611 getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
612 const std::string& cpuId, const std::string& service,
613 const std::string& objPath)
Jonathan Domandba0c292020-12-02 15:34:13 -0800614{
Ed Tanous62598e32023-07-17 17:06:25 -0700615 BMCWEB_LOG_INFO("Getting CPU operating configs for {}", cpuId);
Jonathan Domandba0c292020-12-02 15:34:13 -0800616
617 // First, GetAll CurrentOperatingConfig properties on the object
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200618 sdbusplus::asio::getAllProperties(
619 *crow::connections::systemBus, service, objPath,
620 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Ed Tanousac106bf2023-06-07 09:24:59 -0700621 [asyncResp, cpuId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800622 service](const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200623 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 if (ec)
625 {
Ed Tanous62598e32023-07-17 17:06:25 -0700626 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
Ed Tanousac106bf2023-06-07 09:24:59 -0700627 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700628 return;
629 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800630
Ed Tanousac106bf2023-06-07 09:24:59 -0700631 nlohmann::json& json = asyncResp->res.jsonValue;
Jonathan Domandba0c292020-12-02 15:34:13 -0800632
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200633 const sdbusplus::message::object_path* appliedConfig = nullptr;
634 const bool* baseSpeedPriorityEnabled = nullptr;
635
636 const bool success = sdbusplus::unpackPropertiesNoThrow(
637 dbus_utils::UnpackErrorPrinter(), properties, "AppliedConfig",
638 appliedConfig, "BaseSpeedPriorityEnabled",
639 baseSpeedPriorityEnabled);
640
641 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700642 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700643 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200644 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700645 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200646
647 if (appliedConfig != nullptr)
648 {
649 const std::string& dbusPath = appliedConfig->str;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200650 nlohmann::json::object_t operatingConfig;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700651 operatingConfig["@odata.id"] = boost::urls::format(
652 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs",
653 cpuId);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200654 json["OperatingConfigs"] = std::move(operatingConfig);
655
656 // Reuse the D-Bus config object name for the Redfish
657 // URI
658 size_t baseNamePos = dbusPath.rfind('/');
659 if (baseNamePos == std::string::npos ||
660 baseNamePos == (dbusPath.size() - 1))
661 {
662 // If the AppliedConfig was somehow not a valid path,
663 // skip adding any more properties, since everything
664 // else is tied to this applied config.
Ed Tanousac106bf2023-06-07 09:24:59 -0700665 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200666 return;
667 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200668 nlohmann::json::object_t appliedOperatingConfig;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700669 appliedOperatingConfig["@odata.id"] = boost::urls::format(
670 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs/{}",
671 cpuId, dbusPath.substr(baseNamePos + 1));
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200672 json["AppliedOperatingConfig"] = std::move(appliedOperatingConfig);
673
674 // Once we found the current applied config, queue another
675 // request to read the base freq core ids out of that
676 // config.
677 sdbusplus::asio::getProperty<BaseSpeedPrioritySettingsProperty>(
678 *crow::connections::systemBus, service, dbusPath,
679 "xyz.openbmc_project.Inventory.Item.Cpu."
680 "OperatingConfig",
681 "BaseSpeedPrioritySettings",
Ed Tanousac106bf2023-06-07 09:24:59 -0700682 [asyncResp](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800683 const boost::system::error_code& ec2,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200684 const BaseSpeedPrioritySettingsProperty& baseSpeedList) {
685 if (ec2)
686 {
Ed Tanous62598e32023-07-17 17:06:25 -0700687 BMCWEB_LOG_WARNING("D-Bus Property Get error: {}", ec2);
Ed Tanousac106bf2023-06-07 09:24:59 -0700688 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200689 return;
690 }
691
Ed Tanousac106bf2023-06-07 09:24:59 -0700692 highSpeedCoreIdsHandler(asyncResp, baseSpeedList);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200693 });
694 }
695
696 if (baseSpeedPriorityEnabled != nullptr)
697 {
698 json["BaseSpeedPriorityState"] =
699 *baseSpeedPriorityEnabled ? "Enabled" : "Disabled";
700 }
701 });
Jonathan Domandba0c292020-12-02 15:34:13 -0800702}
703
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600704/**
705 * @brief Fill out location info of a processor by
706 * requesting data from the given D-Bus object.
707 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700708 * @param[in,out] asyncResp Async HTTP response.
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600709 * @param[in] service D-Bus service to query.
710 * @param[in] objPath D-Bus object to query.
711 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700712inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600713 const std::string& service,
714 const std::string& objPath)
715{
Ed Tanous62598e32023-07-17 17:06:25 -0700716 BMCWEB_LOG_DEBUG("Get Cpu Location Data");
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700717 sdbusplus::asio::getProperty<std::string>(
718 *crow::connections::systemBus, service, objPath,
719 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -0700720 [objPath, asyncResp{std::move(asyncResp)}](
721 const boost::system::error_code& ec, const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700722 if (ec)
723 {
Ed Tanous62598e32023-07-17 17:06:25 -0700724 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700725 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700726 return;
727 }
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600728
Ed Tanousac106bf2023-06-07 09:24:59 -0700729 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -0700730 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700731 });
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600732}
733
Jonathan Domanc9514482021-02-24 09:20:51 -0800734/**
Jonathan Doman49e429c2021-03-03 13:11:44 -0800735 * Populate the unique identifier in a Processor resource by requesting data
736 * from the given D-Bus object.
737 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700738 * @param[in,out] asyncResp Async HTTP response.
Jonathan Doman49e429c2021-03-03 13:11:44 -0800739 * @param[in] service D-Bus service to query.
740 * @param[in] objPath D-Bus object to query.
741 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700742inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Jonathan Doman49e429c2021-03-03 13:11:44 -0800743 const std::string& service,
744 const std::string& objectPath)
745{
Ed Tanous62598e32023-07-17 17:06:25 -0700746 BMCWEB_LOG_DEBUG("Get CPU UniqueIdentifier");
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700747 sdbusplus::asio::getProperty<std::string>(
748 *crow::connections::systemBus, service, objectPath,
749 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
750 "UniqueIdentifier",
Ed Tanousac106bf2023-06-07 09:24:59 -0700751 [asyncResp](const boost::system::error_code& ec,
752 const std::string& id) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700753 if (ec)
754 {
Ed Tanous62598e32023-07-17 17:06:25 -0700755 BMCWEB_LOG_ERROR("Failed to read cpu unique id: {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -0700756 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700757 return;
758 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700759 asyncResp->res
760 .jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] = id;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700761 });
Jonathan Doman49e429c2021-03-03 13:11:44 -0800762}
763
764/**
Jonathan Domanc9514482021-02-24 09:20:51 -0800765 * Find the D-Bus object representing the requested Processor, and call the
766 * handler with the results. If matching object is not found, add 404 error to
767 * response and don't call the handler.
768 *
769 * @param[in,out] resp Async HTTP response.
770 * @param[in] processorId Redfish Processor Id.
771 * @param[in] handler Callback to continue processing request upon
772 * successfully finding object.
773 */
774template <typename Handler>
zhanghch058d1b46d2021-04-01 11:18:24 +0800775inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800776 const std::string& processorId,
777 Handler&& handler)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500778{
Ed Tanous62598e32023-07-17 17:06:25 -0700779 BMCWEB_LOG_DEBUG("Get available system processor resources.");
Gunnar Millsac6a4442020-10-14 14:55:29 -0500780
Jonathan Domanc9514482021-02-24 09:20:51 -0800781 // GetSubTree on all interfaces which provide info about a Processor
Chris Caindfbf7de2023-04-13 16:01:04 -0500782 constexpr std::array<std::string_view, 9> interfaces = {
George Liue99073f2022-12-09 11:06:16 +0800783 "xyz.openbmc_project.Common.UUID",
784 "xyz.openbmc_project.Inventory.Decorator.Asset",
785 "xyz.openbmc_project.Inventory.Decorator.Revision",
786 "xyz.openbmc_project.Inventory.Item.Cpu",
787 "xyz.openbmc_project.Inventory.Decorator.LocationCode",
788 "xyz.openbmc_project.Inventory.Item.Accelerator",
789 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Chris Caindfbf7de2023-04-13 16:01:04 -0500790 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
791 "xyz.openbmc_project.Control.Power.Throttle"};
George Liue99073f2022-12-09 11:06:16 +0800792 dbus::utility::getSubTree(
793 "/xyz/openbmc_project/inventory", 0, interfaces,
Jonathan Domanc9514482021-02-24 09:20:51 -0800794 [resp, processorId, handler = std::forward<Handler>(handler)](
George Liue99073f2022-12-09 11:06:16 +0800795 const boost::system::error_code& ec,
796 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700797 if (ec)
798 {
Ed Tanous62598e32023-07-17 17:06:25 -0700799 BMCWEB_LOG_DEBUG("DBUS response error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700800 messages::internalError(resp->res);
801 return;
802 }
803 for (const auto& [objectPath, serviceMap] : subtree)
804 {
805 // Ignore any objects which don't end with our desired cpu name
Ed Tanous11ba3972022-07-11 09:50:41 -0700806 if (!objectPath.ends_with(processorId))
Gunnar Millsac6a4442020-10-14 14:55:29 -0500807 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700808 continue;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500809 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700810
811 bool found = false;
812 // Filter out objects that don't have the CPU-specific
813 // interfaces to make sure we can return 404 on non-CPUs
814 // (e.g. /redfish/../Processors/dimm0)
815 for (const auto& [serviceName, interfaceList] : serviceMap)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500816 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700817 if (std::find_first_of(
818 interfaceList.begin(), interfaceList.end(),
819 processorInterfaces.begin(),
820 processorInterfaces.end()) != interfaceList.end())
Gunnar Millsac6a4442020-10-14 14:55:29 -0500821 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700822 found = true;
823 break;
Jonathan Doman2bab9832020-12-02 15:27:40 -0800824 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500825 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700826
827 if (!found)
828 {
829 continue;
830 }
831
832 // Process the first object which does match our cpu name and
833 // required interfaces, and potentially ignore any other
834 // matching objects. Assume all interfaces we want to process
835 // must be on the same object path.
836
Ed Tanous8a592812022-06-04 09:06:59 -0700837 handler(objectPath, serviceMap);
Ed Tanous002d39b2022-05-31 08:59:27 -0700838 return;
839 }
840 messages::resourceNotFound(resp->res, "Processor", processorId);
George Liue99073f2022-12-09 11:06:16 +0800841 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500842}
843
Ed Tanousac106bf2023-06-07 09:24:59 -0700844inline void
845 getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
846 const std::string& processorId,
847 const std::string& objectPath,
848 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Domanc9514482021-02-24 09:20:51 -0800849{
850 for (const auto& [serviceName, interfaceList] : serviceMap)
851 {
852 for (const auto& interface : interfaceList)
853 {
854 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
855 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700856 getCpuAssetData(asyncResp, serviceName, objectPath);
Jonathan Domanc9514482021-02-24 09:20:51 -0800857 }
George Liu0fda0f12021-11-16 10:06:17 +0800858 else if (interface ==
859 "xyz.openbmc_project.Inventory.Decorator.Revision")
Jonathan Domanc9514482021-02-24 09:20:51 -0800860 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700861 getCpuRevisionData(asyncResp, serviceName, objectPath);
Jonathan Domanc9514482021-02-24 09:20:51 -0800862 }
863 else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
864 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700865 getCpuDataByService(asyncResp, processorId, serviceName,
Jonathan Domanc9514482021-02-24 09:20:51 -0800866 objectPath);
867 }
George Liu0fda0f12021-11-16 10:06:17 +0800868 else if (interface ==
869 "xyz.openbmc_project.Inventory.Item.Accelerator")
Jonathan Domanc9514482021-02-24 09:20:51 -0800870 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700871 getAcceleratorDataByService(asyncResp, processorId, serviceName,
Jonathan Domanc9514482021-02-24 09:20:51 -0800872 objectPath);
873 }
George Liu0fda0f12021-11-16 10:06:17 +0800874 else if (
875 interface ==
876 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig")
Jonathan Domanc9514482021-02-24 09:20:51 -0800877 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700878 getCpuConfigData(asyncResp, processorId, serviceName,
879 objectPath);
Jonathan Domanc9514482021-02-24 09:20:51 -0800880 }
George Liu0fda0f12021-11-16 10:06:17 +0800881 else if (interface ==
882 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
Jonathan Domanc9514482021-02-24 09:20:51 -0800883 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700884 getCpuLocationCode(asyncResp, serviceName, objectPath);
Jonathan Domanc9514482021-02-24 09:20:51 -0800885 }
Sharad Yadav71b82f22021-05-10 15:11:39 +0530886 else if (interface == "xyz.openbmc_project.Common.UUID")
887 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700888 getProcessorUUID(asyncResp, serviceName, objectPath);
Sharad Yadav71b82f22021-05-10 15:11:39 +0530889 }
George Liu0fda0f12021-11-16 10:06:17 +0800890 else if (interface ==
891 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier")
Jonathan Doman49e429c2021-03-03 13:11:44 -0800892 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700893 getCpuUniqueId(asyncResp, serviceName, objectPath);
Jonathan Doman49e429c2021-03-03 13:11:44 -0800894 }
Chris Caindfbf7de2023-04-13 16:01:04 -0500895 else if (interface == "xyz.openbmc_project.Control.Power.Throttle")
896 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700897 getThrottleProperties(asyncResp, serviceName, objectPath);
Chris Caindfbf7de2023-04-13 16:01:04 -0500898 }
Jonathan Domanc9514482021-02-24 09:20:51 -0800899 }
900 }
901}
902
Jonathan Domandba0c292020-12-02 15:34:13 -0800903/**
904 * Request all the properties for the given D-Bus object and fill out the
905 * related entries in the Redfish OperatingConfig response.
906 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700907 * @param[in,out] asyncResp Async HTTP response.
Jonathan Domandba0c292020-12-02 15:34:13 -0800908 * @param[in] service D-Bus service name to query.
909 * @param[in] objPath D-Bus object to query.
910 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800911inline void
Ed Tanousac106bf2023-06-07 09:24:59 -0700912 getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800913 const std::string& service,
914 const std::string& objPath)
Jonathan Domandba0c292020-12-02 15:34:13 -0800915{
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200916 sdbusplus::asio::getAllProperties(
917 *crow::connections::systemBus, service, objPath,
918 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig",
Ed Tanousac106bf2023-06-07 09:24:59 -0700919 [asyncResp](const boost::system::error_code& ec,
920 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700921 if (ec)
922 {
Ed Tanous62598e32023-07-17 17:06:25 -0700923 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
Ed Tanousac106bf2023-06-07 09:24:59 -0700924 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700925 return;
926 }
927
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200928 const size_t* availableCoreCount = nullptr;
929 const uint32_t* baseSpeed = nullptr;
930 const uint32_t* maxJunctionTemperature = nullptr;
931 const uint32_t* maxSpeed = nullptr;
932 const uint32_t* powerLimit = nullptr;
933 const TurboProfileProperty* turboProfile = nullptr;
934 const BaseSpeedPrioritySettingsProperty* baseSpeedPrioritySettings =
935 nullptr;
936
937 const bool success = sdbusplus::unpackPropertiesNoThrow(
938 dbus_utils::UnpackErrorPrinter(), properties, "AvailableCoreCount",
939 availableCoreCount, "BaseSpeed", baseSpeed,
940 "MaxJunctionTemperature", maxJunctionTemperature, "MaxSpeed",
941 maxSpeed, "PowerLimit", powerLimit, "TurboProfile", turboProfile,
942 "BaseSpeedPrioritySettings", baseSpeedPrioritySettings);
943
944 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700945 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700946 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200947 return;
948 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700949
Ed Tanousac106bf2023-06-07 09:24:59 -0700950 nlohmann::json& json = asyncResp->res.jsonValue;
Ed Tanous002d39b2022-05-31 08:59:27 -0700951
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200952 if (availableCoreCount != nullptr)
953 {
954 json["TotalAvailableCoreCount"] = *availableCoreCount;
955 }
956
957 if (baseSpeed != nullptr)
958 {
959 json["BaseSpeedMHz"] = *baseSpeed;
960 }
961
962 if (maxJunctionTemperature != nullptr)
963 {
964 json["MaxJunctionTemperatureCelsius"] = *maxJunctionTemperature;
965 }
966
967 if (maxSpeed != nullptr)
968 {
969 json["MaxSpeedMHz"] = *maxSpeed;
970 }
971
972 if (powerLimit != nullptr)
973 {
974 json["TDPWatts"] = *powerLimit;
975 }
976
977 if (turboProfile != nullptr)
978 {
979 nlohmann::json& turboArray = json["TurboProfile"];
980 turboArray = nlohmann::json::array();
981 for (const auto& [turboSpeed, coreCount] : *turboProfile)
982 {
983 nlohmann::json::object_t turbo;
984 turbo["ActiveCoreCount"] = coreCount;
985 turbo["MaxSpeedMHz"] = turboSpeed;
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500986 turboArray.emplace_back(std::move(turbo));
Ed Tanous002d39b2022-05-31 08:59:27 -0700987 }
988 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200989
990 if (baseSpeedPrioritySettings != nullptr)
991 {
992 nlohmann::json& baseSpeedArray = json["BaseSpeedPrioritySettings"];
993 baseSpeedArray = nlohmann::json::array();
994 for (const auto& [baseSpeedMhz, coreList] :
995 *baseSpeedPrioritySettings)
996 {
997 nlohmann::json::object_t speed;
998 speed["CoreCount"] = coreList.size();
999 speed["CoreIDs"] = coreList;
1000 speed["BaseSpeedMHz"] = baseSpeedMhz;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001001 baseSpeedArray.emplace_back(std::move(speed));
Krzysztof Grobelny351053f2022-07-28 15:44:22 +02001002 }
1003 }
1004 });
Jonathan Domandba0c292020-12-02 15:34:13 -08001005}
1006
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001007/**
1008 * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
1009 * property. Main task is to translate error messages into Redfish errors.
1010 *
1011 * @param[in,out] resp HTTP response.
1012 * @param[in] setPropVal Value which we attempted to set.
1013 * @param[in] ec D-Bus response error code.
1014 * @param[in] msg D-Bus response message.
1015 */
1016inline void
1017 handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
1018 const std::string& setPropVal,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001019 const boost::system::error_code& ec,
Patrick Williams59d494e2022-07-22 19:26:55 -05001020 const sdbusplus::message_t& msg)
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001021{
1022 if (!ec)
1023 {
Ed Tanous62598e32023-07-17 17:06:25 -07001024 BMCWEB_LOG_DEBUG("Set Property succeeded");
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001025 return;
1026 }
1027
Ed Tanous62598e32023-07-17 17:06:25 -07001028 BMCWEB_LOG_DEBUG("Set Property failed: {}", ec);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001029
1030 const sd_bus_error* dbusError = msg.get_error();
1031 if (dbusError == nullptr)
1032 {
1033 messages::internalError(resp->res);
1034 return;
1035 }
1036
1037 // The asio error code doesn't know about our custom errors, so we have to
1038 // parse the error string. Some of these D-Bus -> Redfish translations are a
1039 // stretch, but it's good to try to communicate something vaguely useful.
1040 if (strcmp(dbusError->name,
1041 "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
1042 {
1043 // Service did not like the object_path we tried to set.
1044 messages::propertyValueIncorrect(
1045 resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
1046 }
1047 else if (strcmp(dbusError->name,
1048 "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
1049 {
1050 // Service indicates we can never change the config for this processor.
1051 messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
1052 }
1053 else if (strcmp(dbusError->name,
1054 "xyz.openbmc_project.Common.Error.Unavailable") == 0)
1055 {
1056 // Service indicates the config cannot be changed right now, but maybe
1057 // in a different system state.
1058 messages::resourceInStandby(resp->res);
1059 }
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001060 else
1061 {
1062 messages::internalError(resp->res);
1063 }
1064}
1065
1066/**
1067 * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
1068 * validation of the input data, and then set the D-Bus property.
1069 *
1070 * @param[in,out] resp Async HTTP response.
1071 * @param[in] processorId Processor's Id.
1072 * @param[in] appliedConfigUri New property value to apply.
1073 * @param[in] cpuObjectPath Path of CPU object to modify.
1074 * @param[in] serviceMap Service map for CPU object.
1075 */
1076inline void patchAppliedOperatingConfig(
1077 const std::shared_ptr<bmcweb::AsyncResp>& resp,
1078 const std::string& processorId, const std::string& appliedConfigUri,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -06001079 const std::string& cpuObjectPath,
1080 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001081{
1082 // Check that the property even exists by checking for the interface
1083 const std::string* controlService = nullptr;
1084 for (const auto& [serviceName, interfaceList] : serviceMap)
1085 {
1086 if (std::find(interfaceList.begin(), interfaceList.end(),
1087 "xyz.openbmc_project.Control.Processor."
1088 "CurrentOperatingConfig") != interfaceList.end())
1089 {
1090 controlService = &serviceName;
1091 break;
1092 }
1093 }
1094
1095 if (controlService == nullptr)
1096 {
1097 messages::internalError(resp->res);
1098 return;
1099 }
1100
1101 // Check that the config URI is a child of the cpu URI being patched.
1102 std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
1103 expectedPrefix += processorId;
1104 expectedPrefix += "/OperatingConfigs/";
Ed Tanous11ba3972022-07-11 09:50:41 -07001105 if (!appliedConfigUri.starts_with(expectedPrefix) ||
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001106 expectedPrefix.size() == appliedConfigUri.size())
1107 {
1108 messages::propertyValueIncorrect(
1109 resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
1110 return;
1111 }
1112
1113 // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
1114 // direct child of the CPU object.
1115 // Strip the expectedPrefix from the config URI to get the "filename", and
1116 // append to the CPU's path.
1117 std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
1118 sdbusplus::message::object_path configPath(cpuObjectPath);
1119 configPath /= configBaseName;
1120
Ed Tanous62598e32023-07-17 17:06:25 -07001121 BMCWEB_LOG_INFO("Setting config to {}", configPath.str);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001122
1123 // Set the property, with handler to check error responses
George Liu9ae226f2023-06-21 17:56:46 +08001124 sdbusplus::asio::setProperty(
1125 *crow::connections::systemBus, *controlService, cpuObjectPath,
1126 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
1127 "AppliedConfig", configPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001128 [resp, appliedConfigUri](const boost::system::error_code& ec,
Patrick Williams59d494e2022-07-22 19:26:55 -05001129 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
George Liu9ae226f2023-06-21 17:56:46 +08001131 });
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001132}
1133
Ed Tanousac106bf2023-06-07 09:24:59 -07001134inline void
1135 handleProcessorHead(crow::App& app, const crow::Request& req,
1136 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1137 const std::string& /* systemName */,
1138 const std::string& /* processorId */)
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001139{
Ed Tanousac106bf2023-06-07 09:24:59 -07001140 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001141 {
1142 return;
1143 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001144 asyncResp->res.addHeader(
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001145 boost::beast::http::field::link,
1146 "</redfish/v1/JsonSchemas/Processor/Processor.json>; rel=describedby");
1147}
1148
1149inline void handleProcessorCollectionHead(
1150 crow::App& app, const crow::Request& req,
Ed Tanousac106bf2023-06-07 09:24:59 -07001151 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001152 const std::string& /* systemName */)
1153{
Ed Tanousac106bf2023-06-07 09:24:59 -07001154 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001155 {
1156 return;
1157 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001158 asyncResp->res.addHeader(
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001159 boost::beast::http::field::link,
1160 "</redfish/v1/JsonSchemas/ProcessorCollection/ProcessorCollection.json>; rel=describedby");
1161}
1162
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001163inline void requestRoutesOperatingConfigCollection(App& app)
Jonathan Domandba0c292020-12-02 15:34:13 -08001164{
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001165 BMCWEB_ROUTE(app,
1166 "/redfish/v1/Systems/<str>/Processors/<str>/OperatingConfigs/")
Ed Tanoused398212021-06-09 17:05:54 -07001167 .privileges(redfish::privileges::getOperatingConfigCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001168 .methods(boost::beast::http::verb::get)(
1169 [&app](const crow::Request& req,
1170 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001171 const std::string& systemName, const std::string& cpuName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001172 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001173 {
1174 return;
1175 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001176
1177 if constexpr (bmcwebEnableMultiHost)
1178 {
1179 // Option currently returns no systems. TBD
1180 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1181 systemName);
1182 return;
1183 }
1184
1185 if (systemName != "system")
1186 {
1187 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1188 systemName);
1189 return;
1190 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001191 asyncResp->res.jsonValue["@odata.type"] =
1192 "#OperatingConfigCollection.OperatingConfigCollection";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001193 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1194 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs",
1195 cpuName);
Ed Tanous002d39b2022-05-31 08:59:27 -07001196 asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
1197
1198 // First find the matching CPU object so we know how to
1199 // constrain our search for related Config objects.
George Liu7a1dbc42022-12-07 16:03:22 +08001200 const std::array<std::string_view, 1> interfaces = {
1201 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"};
1202 dbus::utility::getSubTreePaths(
1203 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001204 [asyncResp, cpuName](
George Liu7a1dbc42022-12-07 16:03:22 +08001205 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
1207 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001208 {
Ed Tanous62598e32023-07-17 17:06:25 -07001209 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07001210 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001211 return;
1212 }
Jonathan Domandba0c292020-12-02 15:34:13 -08001213
Ed Tanous002d39b2022-05-31 08:59:27 -07001214 for (const std::string& object : objects)
1215 {
Ed Tanous11ba3972022-07-11 09:50:41 -07001216 if (!object.ends_with(cpuName))
Ed Tanous002d39b2022-05-31 08:59:27 -07001217 {
1218 continue;
1219 }
George Liu0fda0f12021-11-16 10:06:17 +08001220
Ed Tanous002d39b2022-05-31 08:59:27 -07001221 // Not expected that there will be multiple matching
1222 // CPU objects, but if there are just use the first
1223 // one.
Jonathan Domandba0c292020-12-02 15:34:13 -08001224
Ed Tanous002d39b2022-05-31 08:59:27 -07001225 // Use the common search routine to construct the
1226 // Collection of all Config objects under this CPU.
George Liu7a1dbc42022-12-07 16:03:22 +08001227 constexpr std::array<std::string_view, 1> interface {
1228 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"
1229 };
Ed Tanous002d39b2022-05-31 08:59:27 -07001230 collection_util::getCollectionMembers(
1231 asyncResp,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001232 boost::urls::format(
1233 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs",
1234 cpuName),
George Liu7a1dbc42022-12-07 16:03:22 +08001235 interface, object.c_str());
Ed Tanous002d39b2022-05-31 08:59:27 -07001236 return;
1237 }
George Liu7a1dbc42022-12-07 16:03:22 +08001238 });
George Liu0fda0f12021-11-16 10:06:17 +08001239 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001240}
1241
1242inline void requestRoutesOperatingConfig(App& app)
1243{
1244 BMCWEB_ROUTE(
1245 app,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001246 "/redfish/v1/Systems/<str>/Processors/<str>/OperatingConfigs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001247 .privileges(redfish::privileges::getOperatingConfig)
Ed Tanous002d39b2022-05-31 08:59:27 -07001248 .methods(boost::beast::http::verb::get)(
1249 [&app](const crow::Request& req,
1250 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001251 const std::string& systemName, const std::string& cpuName,
1252 const std::string& configName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001253 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001254 {
1255 return;
1256 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001257 if constexpr (bmcwebEnableMultiHost)
1258 {
1259 // Option currently returns no systems. TBD
1260 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1261 systemName);
1262 return;
1263 }
1264
1265 if (systemName != "system")
1266 {
1267 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1268 systemName);
1269 return;
1270 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001271 // Ask for all objects implementing OperatingConfig so we can search
1272 // for one with a matching name
George Liue99073f2022-12-09 11:06:16 +08001273 constexpr std::array<std::string_view, 1> interfaces = {
1274 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"};
1275 dbus::utility::getSubTree(
1276 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous39662a32023-02-06 15:09:46 -08001277 [asyncResp, cpuName, configName](
George Liue99073f2022-12-09 11:06:16 +08001278 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001279 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1280 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001281 {
Ed Tanous62598e32023-07-17 17:06:25 -07001282 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07001283 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001284 return;
1285 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001286 const std::string expectedEnding = cpuName + '/' + configName;
1287 for (const auto& [objectPath, serviceMap] : subtree)
1288 {
1289 // Ignore any configs without matching cpuX/configY
Ed Tanous11ba3972022-07-11 09:50:41 -07001290 if (!objectPath.ends_with(expectedEnding) || serviceMap.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -07001291 {
1292 continue;
1293 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001294
Ed Tanous002d39b2022-05-31 08:59:27 -07001295 nlohmann::json& json = asyncResp->res.jsonValue;
1296 json["@odata.type"] = "#OperatingConfig.v1_0_0.OperatingConfig";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001297 json["@odata.id"] = boost::urls::format(
1298 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs/{}",
1299 cpuName, configName);
Ed Tanous002d39b2022-05-31 08:59:27 -07001300 json["Name"] = "Processor Profile";
1301 json["Id"] = configName;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001302
Ed Tanous002d39b2022-05-31 08:59:27 -07001303 // Just use the first implementation of the object - not
1304 // expected that there would be multiple matching
1305 // services
1306 getOperatingConfigData(asyncResp, serviceMap.begin()->first,
1307 objectPath);
1308 return;
1309 }
1310 messages::resourceNotFound(asyncResp->res, "OperatingConfig",
1311 configName);
George Liue99073f2022-12-09 11:06:16 +08001312 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001313 });
1314}
Jonathan Domandba0c292020-12-02 15:34:13 -08001315
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001316inline void requestRoutesProcessorCollection(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001317{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001318 /**
1319 * Functions triggers appropriate requests on DBus
1320 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001321 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001322 .privileges(redfish::privileges::headProcessorCollection)
1323 .methods(boost::beast::http::verb::head)(
1324 std::bind_front(handleProcessorCollectionHead, std::ref(app)));
1325
1326 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
Ed Tanoused398212021-06-09 17:05:54 -07001327 .privileges(redfish::privileges::getProcessorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001328 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001329 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001330 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1331 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001332 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001333 {
1334 return;
1335 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001336 if constexpr (bmcwebEnableMultiHost)
1337 {
1338 // Option currently returns no systems. TBD
1339 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1340 systemName);
1341 return;
1342 }
1343
Ed Tanous22d268c2022-05-19 09:39:07 -07001344 if (systemName != "system")
1345 {
1346 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1347 systemName);
1348 return;
1349 }
1350
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001351 asyncResp->res.addHeader(
1352 boost::beast::http::field::link,
1353 "</redfish/v1/JsonSchemas/ProcessorCollection/ProcessorCollection.json>; rel=describedby");
1354
Ed Tanous002d39b2022-05-31 08:59:27 -07001355 asyncResp->res.jsonValue["@odata.type"] =
1356 "#ProcessorCollection.ProcessorCollection";
1357 asyncResp->res.jsonValue["Name"] = "Processor Collection";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001358
Ed Tanous002d39b2022-05-31 08:59:27 -07001359 asyncResp->res.jsonValue["@odata.id"] =
1360 "/redfish/v1/Systems/system/Processors";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001361
Ed Tanous002d39b2022-05-31 08:59:27 -07001362 collection_util::getCollectionMembers(
Willy Tuae9031f2022-09-27 05:48:07 +00001363 asyncResp,
1364 boost::urls::url("/redfish/v1/Systems/system/Processors"),
George Liu7a1dbc42022-12-07 16:03:22 +08001365 processorInterfaces);
Ed Tanous002d39b2022-05-31 08:59:27 -07001366 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001367}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001368
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001369inline void requestRoutesProcessor(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001370{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001371 /**
1372 * Functions triggers appropriate requests on DBus
1373 */
Gunnar Millsac6a4442020-10-14 14:55:29 -05001374
Ed Tanous22d268c2022-05-19 09:39:07 -07001375 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001376 .privileges(redfish::privileges::headProcessor)
1377 .methods(boost::beast::http::verb::head)(
1378 std::bind_front(handleProcessorHead, std::ref(app)));
1379
1380 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001381 .privileges(redfish::privileges::getProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001382 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001383 [&app](const crow::Request& req,
1384 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001385 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001386 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001387 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001388 {
1389 return;
1390 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001391 if constexpr (bmcwebEnableMultiHost)
1392 {
1393 // Option currently returns no systems. TBD
1394 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1395 systemName);
1396 return;
1397 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001398 if (systemName != "system")
1399 {
1400 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1401 systemName);
1402 return;
1403 }
1404
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001405 asyncResp->res.addHeader(
1406 boost::beast::http::field::link,
1407 "</redfish/v1/JsonSchemas/Processor/Processor.json>; rel=describedby");
Ed Tanous002d39b2022-05-31 08:59:27 -07001408 asyncResp->res.jsonValue["@odata.type"] =
Chris Caindfbf7de2023-04-13 16:01:04 -05001409 "#Processor.v1_18_0.Processor";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001410 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1411 "/redfish/v1/Systems/system/Processors/{}", processorId);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001412
Ed Tanous8a592812022-06-04 09:06:59 -07001413 getProcessorObject(
1414 asyncResp, processorId,
1415 std::bind_front(getProcessorData, asyncResp, processorId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 });
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001417
Ed Tanous22d268c2022-05-19 09:39:07 -07001418 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001419 .privileges(redfish::privileges::patchProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001420 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001421 [&app](const crow::Request& req,
1422 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001423 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001424 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001425 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001426 {
1427 return;
1428 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001429 if constexpr (bmcwebEnableMultiHost)
1430 {
1431 // Option currently returns no systems. TBD
1432 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1433 systemName);
1434 return;
1435 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001436 if (systemName != "system")
1437 {
1438 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1439 systemName);
1440 return;
1441 }
1442
Ed Tanous002d39b2022-05-31 08:59:27 -07001443 std::optional<nlohmann::json> appliedConfigJson;
1444 if (!json_util::readJsonPatch(req, asyncResp->res,
1445 "AppliedOperatingConfig",
1446 appliedConfigJson))
1447 {
1448 return;
1449 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001450
Ed Tanous002d39b2022-05-31 08:59:27 -07001451 if (appliedConfigJson)
1452 {
Ed Tanousf8fe53e2022-06-30 15:55:45 -07001453 std::string appliedConfigUri;
Ed Tanous002d39b2022-05-31 08:59:27 -07001454 if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
1455 "@odata.id", appliedConfigUri))
1456 {
1457 return;
1458 }
1459 // Check for 404 and find matching D-Bus object, then run
1460 // property patch handlers if that all succeeds.
Ed Tanous8a592812022-06-04 09:06:59 -07001461 getProcessorObject(asyncResp, processorId,
1462 std::bind_front(patchAppliedOperatingConfig,
1463 asyncResp, processorId,
1464 appliedConfigUri));
Ed Tanous002d39b2022-05-31 08:59:27 -07001465 }
1466 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001467}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001468
1469} // namespace redfish