blob: 6ddc9017562deaa42584a157a5d298a3fd61d3d2 [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>
Ed Tanous3544d2a2023-08-06 18:12:20 -070040#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080041#include <string_view>
42
Gunnar Millsac6a4442020-10-14 14:55:29 -050043namespace redfish
44{
45
Jonathan Domanc9514482021-02-24 09:20:51 -080046// Interfaces which imply a D-Bus object represents a Processor
George Liu7a1dbc42022-12-07 16:03:22 +080047constexpr std::array<std::string_view, 2> processorInterfaces = {
Jonathan Domanc9514482021-02-24 09:20:51 -080048 "xyz.openbmc_project.Inventory.Item.Cpu",
49 "xyz.openbmc_project.Inventory.Item.Accelerator"};
Jonathan Doman2bab9832020-12-02 15:27:40 -080050
Sharad Yadav71b82f22021-05-10 15:11:39 +053051/**
52 * @brief Fill out uuid info of a processor by
53 * requesting data from the given D-Bus object.
54 *
Ed Tanousac106bf2023-06-07 09:24:59 -070055 * @param[in,out] asyncResp Async HTTP response.
Sharad Yadav71b82f22021-05-10 15:11:39 +053056 * @param[in] service D-Bus service to query.
57 * @param[in] objPath D-Bus object to query.
58 */
Ed Tanousac106bf2023-06-07 09:24:59 -070059inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Sharad Yadav71b82f22021-05-10 15:11:39 +053060 const std::string& service,
61 const std::string& objPath)
62{
Ed Tanous62598e32023-07-17 17:06:25 -070063 BMCWEB_LOG_DEBUG("Get Processor UUID");
Jonathan Doman1e1e5982021-06-11 09:36:17 -070064 sdbusplus::asio::getProperty<std::string>(
65 *crow::connections::systemBus, service, objPath,
66 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanousac106bf2023-06-07 09:24:59 -070067 [objPath, asyncResp{std::move(asyncResp)}](
68 const boost::system::error_code& ec, const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -070069 if (ec)
70 {
Ed Tanous62598e32023-07-17 17:06:25 -070071 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -070072 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -070073 return;
74 }
Ed Tanousac106bf2023-06-07 09:24:59 -070075 asyncResp->res.jsonValue["UUID"] = property;
Patrick Williams5a39f772023-10-20 11:20:21 -050076 });
Sharad Yadav71b82f22021-05-10 15:11:39 +053077}
78
Ed Tanous711ac7a2021-12-20 09:34:41 -080079inline void getCpuDataByInterface(
Ed Tanousac106bf2023-06-07 09:24:59 -070080 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Michael Shen80f79a42023-08-24 13:41:53 +000081 const dbus::utility::DBusInterfacesMap& cpuInterfacesProperties)
Gunnar Millsac6a4442020-10-14 14:55:29 -050082{
Ed Tanous62598e32023-07-17 17:06:25 -070083 BMCWEB_LOG_DEBUG("Get CPU resources by interface.");
Gunnar Millsac6a4442020-10-14 14:55:29 -050084
Chicago Duana1649ec2021-03-30 16:54:58 +080085 // Set the default value of state
Ed Tanousac106bf2023-06-07 09:24:59 -070086 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
87 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -050088
89 for (const auto& interface : cpuInterfacesProperties)
90 {
91 for (const auto& property : interface.second)
92 {
Chicago Duana1649ec2021-03-30 16:54:58 +080093 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -050094 {
Chicago Duana1649ec2021-03-30 16:54:58 +080095 const bool* cpuPresent = std::get_if<bool>(&property.second);
96 if (cpuPresent == nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -050097 {
98 // Important property not in desired type
Ed Tanousac106bf2023-06-07 09:24:59 -070099 messages::internalError(asyncResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500100 return;
101 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800102 if (!*cpuPresent)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500103 {
Chicago Duana1649ec2021-03-30 16:54:58 +0800104 // Slot is not populated
Ed Tanousac106bf2023-06-07 09:24:59 -0700105 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
Chicago Duana1649ec2021-03-30 16:54:58 +0800106 }
107 }
108 else if (property.first == "Functional")
109 {
110 const bool* cpuFunctional = std::get_if<bool>(&property.second);
111 if (cpuFunctional == nullptr)
112 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700113 messages::internalError(asyncResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500114 return;
115 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800116 if (!*cpuFunctional)
Chicago Duana1649ec2021-03-30 16:54:58 +0800117 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700118 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
Chicago Duana1649ec2021-03-30 16:54:58 +0800119 }
120 }
121 else if (property.first == "CoreCount")
122 {
123 const uint16_t* coresCount =
124 std::get_if<uint16_t>(&property.second);
125 if (coresCount == nullptr)
126 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700127 messages::internalError(asyncResp->res);
Chicago Duana1649ec2021-03-30 16:54:58 +0800128 return;
129 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700130 asyncResp->res.jsonValue["TotalCores"] = *coresCount;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500131 }
Jonathan Domandc3fa662020-10-26 23:10:24 -0700132 else if (property.first == "MaxSpeedInMhz")
133 {
134 const uint32_t* value = std::get_if<uint32_t>(&property.second);
135 if (value != nullptr)
136 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700137 asyncResp->res.jsonValue["MaxSpeedMHz"] = *value;
Jonathan Domandc3fa662020-10-26 23:10:24 -0700138 }
139 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500140 else if (property.first == "Socket")
141 {
142 const std::string* value =
143 std::get_if<std::string>(&property.second);
144 if (value != nullptr)
145 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700146 asyncResp->res.jsonValue["Socket"] = *value;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500147 }
148 }
149 else if (property.first == "ThreadCount")
150 {
Jonathan Domandc3fa662020-10-26 23:10:24 -0700151 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500152 if (value != nullptr)
153 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700154 asyncResp->res.jsonValue["TotalThreads"] = *value;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500155 }
156 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700157 else if (property.first == "EffectiveFamily")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500158 {
Brandon Kim1930fbd2021-09-14 17:52:51 -0700159 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Brad Bishop6169de22022-09-14 13:08:32 -0400160 if (value != nullptr && *value != 2)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500161 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700162 asyncResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800163 "0x" + intToHexString(*value, 4);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500164 }
165 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700166 else if (property.first == "EffectiveModel")
167 {
168 const uint16_t* value = std::get_if<uint16_t>(&property.second);
169 if (value == nullptr)
170 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700171 messages::internalError(asyncResp->res);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700172 return;
173 }
Brad Bishop6169de22022-09-14 13:08:32 -0400174 if (*value != 0)
175 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700176 asyncResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
Brad Bishop6169de22022-09-14 13:08:32 -0400177 "0x" + intToHexString(*value, 4);
178 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700179 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500180 else if (property.first == "Id")
181 {
182 const uint64_t* value = std::get_if<uint64_t>(&property.second);
183 if (value != nullptr && *value != 0)
184 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700185 asyncResp->res
Gunnar Millsac6a4442020-10-14 14:55:29 -0500186 .jsonValue["ProcessorId"]["IdentificationRegisters"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800187 "0x" + intToHexString(*value, 16);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500188 }
189 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700190 else if (property.first == "Microcode")
191 {
192 const uint32_t* value = std::get_if<uint32_t>(&property.second);
193 if (value == nullptr)
194 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700195 messages::internalError(asyncResp->res);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700196 return;
197 }
Brad Bishop6169de22022-09-14 13:08:32 -0400198 if (*value != 0)
199 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700200 asyncResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
Brad Bishop6169de22022-09-14 13:08:32 -0400201 "0x" + intToHexString(*value, 8);
202 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700203 }
204 else if (property.first == "Step")
205 {
206 const uint16_t* value = std::get_if<uint16_t>(&property.second);
207 if (value == nullptr)
208 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700209 messages::internalError(asyncResp->res);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700210 return;
211 }
Michael Shenb9d679d2023-02-13 02:29:04 +0000212 if (*value != std::numeric_limits<uint16_t>::max())
Brad Bishop6169de22022-09-14 13:08:32 -0400213 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700214 asyncResp->res.jsonValue["ProcessorId"]["Step"] =
Brad Bishop6169de22022-09-14 13:08:32 -0400215 "0x" + intToHexString(*value, 4);
216 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700217 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500218 }
219 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500220}
221
Ed Tanousac106bf2023-06-07 09:24:59 -0700222inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500223 const std::string& cpuId,
224 const std::string& service,
225 const std::string& objPath)
226{
Ed Tanous62598e32023-07-17 17:06:25 -0700227 BMCWEB_LOG_DEBUG("Get available system cpu resources by service.");
Gunnar Millsac6a4442020-10-14 14:55:29 -0500228
George Liu5eb468d2023-06-20 17:03:24 +0800229 sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
230 dbus::utility::getManagedObjects(
231 service, path,
Ed Tanousac106bf2023-06-07 09:24:59 -0700232 [cpuId, service, objPath, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800233 const boost::system::error_code& ec,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500234 const dbus::utility::ManagedObjectType& dbusData) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700235 if (ec)
236 {
Ed Tanous62598e32023-07-17 17:06:25 -0700237 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700238 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700239 return;
240 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700241 asyncResp->res.jsonValue["Id"] = cpuId;
242 asyncResp->res.jsonValue["Name"] = "Processor";
243 asyncResp->res.jsonValue["ProcessorType"] = "CPU";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500244
Ed Tanous002d39b2022-05-31 08:59:27 -0700245 bool slotPresent = false;
246 std::string corePath = objPath + "/core";
247 size_t totalCores = 0;
248 for (const auto& object : dbusData)
249 {
250 if (object.first.str == objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500251 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700252 getCpuDataByInterface(asyncResp, object.second);
Ed Tanous002d39b2022-05-31 08:59:27 -0700253 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700254 else if (object.first.str.starts_with(corePath))
Ed Tanous002d39b2022-05-31 08:59:27 -0700255 {
256 for (const auto& interface : object.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500257 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700258 if (interface.first == "xyz.openbmc_project.Inventory.Item")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500259 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700260 for (const auto& property : interface.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500261 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700262 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500263 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700264 const bool* present =
265 std::get_if<bool>(&property.second);
266 if (present != nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500267 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700268 if (*present)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500269 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700270 slotPresent = true;
271 totalCores++;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500272 }
273 }
274 }
275 }
276 }
277 }
278 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700279 }
280 // In getCpuDataByInterface(), state and health are set
281 // based on the present and functional status. If core
282 // count is zero, then it has a higher precedence.
283 if (slotPresent)
284 {
285 if (totalCores == 0)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500286 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700287 // Slot is not populated, set status end return
Ed Tanousac106bf2023-06-07 09:24:59 -0700288 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
289 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500290 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700291 asyncResp->res.jsonValue["TotalCores"] = totalCores;
Ed Tanous002d39b2022-05-31 08:59:27 -0700292 }
293 return;
Patrick Williams5a39f772023-10-20 11:20:21 -0500294 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500295}
296
Chris Caindfbf7de2023-04-13 16:01:04 -0500297/**
298 * @brief Translates throttle cause DBUS property to redfish.
299 *
300 * @param[in] dbusSource The throttle cause from DBUS
301 *
302 * @return Returns as a string, the throttle cause in Redfish terms. If
303 * translation cannot be done, returns "Unknown" throttle reason.
304 */
305inline processor::ThrottleCause
306 dbusToRfThrottleCause(const std::string& dbusSource)
307{
308 if (dbusSource ==
309 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.ClockLimit")
310 {
311 return processor::ThrottleCause::ClockLimit;
312 }
313 if (dbusSource ==
314 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.ManagementDetectedFault")
315 {
316 return processor::ThrottleCause::ManagementDetectedFault;
317 }
318 if (dbusSource ==
319 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.PowerLimit")
320 {
321 return processor::ThrottleCause::PowerLimit;
322 }
323 if (dbusSource ==
324 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.ThermalLimit")
325 {
326 return processor::ThrottleCause::ThermalLimit;
327 }
328 if (dbusSource ==
329 "xyz.openbmc_project.Control.Power.Throttle.ThrottleReasons.Unknown")
330 {
331 return processor::ThrottleCause::Unknown;
332 }
333 return processor::ThrottleCause::Invalid;
334}
335
336inline void
Ed Tanousac106bf2023-06-07 09:24:59 -0700337 readThrottleProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Chris Caindfbf7de2023-04-13 16:01:04 -0500338 const boost::system::error_code& ec,
339 const dbus::utility::DBusPropertiesMap& properties)
340{
341 if (ec)
342 {
Ed Tanous62598e32023-07-17 17:06:25 -0700343 BMCWEB_LOG_ERROR("Processor Throttle getAllProperties error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -0700344 messages::internalError(asyncResp->res);
Chris Caindfbf7de2023-04-13 16:01:04 -0500345 return;
346 }
347
348 const bool* status = nullptr;
349 const std::vector<std::string>* causes = nullptr;
350
351 if (!sdbusplus::unpackPropertiesNoThrow(dbus_utils::UnpackErrorPrinter(),
352 properties, "Throttled", status,
353 "ThrottleCauses", causes))
354 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700355 messages::internalError(asyncResp->res);
Chris Caindfbf7de2023-04-13 16:01:04 -0500356 return;
357 }
358
Ed Tanousac106bf2023-06-07 09:24:59 -0700359 asyncResp->res.jsonValue["Throttled"] = *status;
Chris Caindfbf7de2023-04-13 16:01:04 -0500360 nlohmann::json::array_t rCauses;
361 for (const std::string& cause : *causes)
362 {
363 processor::ThrottleCause rfCause = dbusToRfThrottleCause(cause);
364 if (rfCause == processor::ThrottleCause::Invalid)
365 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700366 messages::internalError(asyncResp->res);
Chris Caindfbf7de2023-04-13 16:01:04 -0500367 return;
368 }
369
370 rCauses.emplace_back(rfCause);
371 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700372 asyncResp->res.jsonValue["ThrottleCauses"] = std::move(rCauses);
Chris Caindfbf7de2023-04-13 16:01:04 -0500373}
374
375inline void
Ed Tanousac106bf2023-06-07 09:24:59 -0700376 getThrottleProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Chris Caindfbf7de2023-04-13 16:01:04 -0500377 const std::string& service,
378 const std::string& objectPath)
379{
Ed Tanous62598e32023-07-17 17:06:25 -0700380 BMCWEB_LOG_DEBUG("Get processor throttle resources");
Chris Caindfbf7de2023-04-13 16:01:04 -0500381
382 sdbusplus::asio::getAllProperties(
383 *crow::connections::systemBus, service, objectPath,
384 "xyz.openbmc_project.Control.Power.Throttle",
Ed Tanousac106bf2023-06-07 09:24:59 -0700385 [asyncResp](const boost::system::error_code& ec,
386 const dbus::utility::DBusPropertiesMap& properties) {
387 readThrottleProperties(asyncResp, ec, properties);
Patrick Williams5a39f772023-10-20 11:20:21 -0500388 });
Chris Caindfbf7de2023-04-13 16:01:04 -0500389}
390
Ed Tanousac106bf2023-06-07 09:24:59 -0700391inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500392 const std::string& service,
393 const std::string& objPath)
394{
Ed Tanous62598e32023-07-17 17:06:25 -0700395 BMCWEB_LOG_DEBUG("Get Cpu Asset Data");
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200396 sdbusplus::asio::getAllProperties(
397 *crow::connections::systemBus, service, objPath,
398 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanousac106bf2023-06-07 09:24:59 -0700399 [objPath, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800400 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200401 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700402 if (ec)
403 {
Ed Tanous62598e32023-07-17 17:06:25 -0700404 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700405 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700406 return;
407 }
408
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200409 const std::string* serialNumber = nullptr;
410 const std::string* model = nullptr;
411 const std::string* manufacturer = nullptr;
412 const std::string* partNumber = nullptr;
413 const std::string* sparePartNumber = nullptr;
414
415 const bool success = sdbusplus::unpackPropertiesNoThrow(
416 dbus_utils::UnpackErrorPrinter(), properties, "SerialNumber",
417 serialNumber, "Model", model, "Manufacturer", manufacturer,
418 "PartNumber", partNumber, "SparePartNumber", sparePartNumber);
419
420 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700421 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700422 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200423 return;
424 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700425
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200426 if (serialNumber != nullptr && !serialNumber->empty())
427 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700428 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200429 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700430
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200431 if ((model != nullptr) && !model->empty())
432 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700433 asyncResp->res.jsonValue["Model"] = *model;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200434 }
435
436 if (manufacturer != nullptr)
437 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700438 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200439
440 // Otherwise would be unexpected.
441 if (manufacturer->find("Intel") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700442 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700443 asyncResp->res.jsonValue["ProcessorArchitecture"] = "x86";
444 asyncResp->res.jsonValue["InstructionSet"] = "x86-64";
Ed Tanous002d39b2022-05-31 08:59:27 -0700445 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200446 else if (manufacturer->find("IBM") != std::string::npos)
Ed Tanous002d39b2022-05-31 08:59:27 -0700447 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700448 asyncResp->res.jsonValue["ProcessorArchitecture"] = "Power";
449 asyncResp->res.jsonValue["InstructionSet"] = "PowerISA";
Ed Tanous002d39b2022-05-31 08:59:27 -0700450 }
451 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200452
453 if (partNumber != nullptr)
454 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700455 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200456 }
457
Brad Bishop6169de22022-09-14 13:08:32 -0400458 if (sparePartNumber != nullptr && !sparePartNumber->empty())
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200459 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700460 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200461 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500462 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500463}
464
Ed Tanousac106bf2023-06-07 09:24:59 -0700465inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500466 const std::string& service,
467 const std::string& objPath)
468{
Ed Tanous62598e32023-07-17 17:06:25 -0700469 BMCWEB_LOG_DEBUG("Get Cpu Revision Data");
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200470 sdbusplus::asio::getAllProperties(
471 *crow::connections::systemBus, service, objPath,
472 "xyz.openbmc_project.Inventory.Decorator.Revision",
Ed Tanousac106bf2023-06-07 09:24:59 -0700473 [objPath, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800474 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200475 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700476 if (ec)
477 {
Ed Tanous62598e32023-07-17 17:06:25 -0700478 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700479 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700480 return;
481 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500482
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200483 const std::string* version = nullptr;
484
485 const bool success = sdbusplus::unpackPropertiesNoThrow(
486 dbus_utils::UnpackErrorPrinter(), properties, "Version", version);
487
488 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700489 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700490 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200491 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700492 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200493
494 if (version != nullptr)
495 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700496 asyncResp->res.jsonValue["Version"] = *version;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200497 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500498 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500499}
500
zhanghch058d1b46d2021-04-01 11:18:24 +0800501inline void getAcceleratorDataByService(
Ed Tanousac106bf2023-06-07 09:24:59 -0700502 std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& acclrtrId,
zhanghch058d1b46d2021-04-01 11:18:24 +0800503 const std::string& service, const std::string& objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500504{
Ed Tanous62598e32023-07-17 17:06:25 -0700505 BMCWEB_LOG_DEBUG("Get available system Accelerator resources by service.");
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200506 sdbusplus::asio::getAllProperties(
507 *crow::connections::systemBus, service, objPath, "",
Ed Tanousac106bf2023-06-07 09:24:59 -0700508 [acclrtrId, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800509 const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200510 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700511 if (ec)
512 {
Ed Tanous62598e32023-07-17 17:06:25 -0700513 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700514 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700515 return;
516 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700517
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200518 const bool* functional = nullptr;
519 const bool* present = nullptr;
520
521 const bool success = sdbusplus::unpackPropertiesNoThrow(
522 dbus_utils::UnpackErrorPrinter(), properties, "Functional",
523 functional, "Present", present);
524
525 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700526 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700527 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200528 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700529 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500530
Ed Tanous002d39b2022-05-31 08:59:27 -0700531 std::string state = "Enabled";
532 std::string health = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500533
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200534 if (present != nullptr && !*present)
Ed Tanous002d39b2022-05-31 08:59:27 -0700535 {
536 state = "Absent";
537 }
538
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200539 if (functional != nullptr && !*functional)
Ed Tanous002d39b2022-05-31 08:59:27 -0700540 {
541 if (state == "Enabled")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500542 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 health = "Critical";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500544 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700545 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500546
Ed Tanousac106bf2023-06-07 09:24:59 -0700547 asyncResp->res.jsonValue["Id"] = acclrtrId;
548 asyncResp->res.jsonValue["Name"] = "Processor";
549 asyncResp->res.jsonValue["Status"]["State"] = state;
550 asyncResp->res.jsonValue["Status"]["Health"] = health;
551 asyncResp->res.jsonValue["ProcessorType"] = "Accelerator";
Patrick Williams5a39f772023-10-20 11:20:21 -0500552 });
Gunnar Millsac6a4442020-10-14 14:55:29 -0500553}
554
Jonathan Domandba0c292020-12-02 15:34:13 -0800555// OperatingConfig D-Bus Types
556using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
557using BaseSpeedPrioritySettingsProperty =
558 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
559// uint32_t and size_t may or may not be the same type, requiring a dedup'd
560// variant
Jonathan Domandba0c292020-12-02 15:34:13 -0800561
562/**
563 * Fill out the HighSpeedCoreIDs in a Processor resource from the given
564 * OperatingConfig D-Bus property.
565 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700566 * @param[in,out] asyncResp Async HTTP response.
Jonathan Domandba0c292020-12-02 15:34:13 -0800567 * @param[in] baseSpeedSettings Full list of base speed priority groups,
568 * to use to determine the list of high
569 * speed cores.
570 */
571inline void highSpeedCoreIdsHandler(
Ed Tanousac106bf2023-06-07 09:24:59 -0700572 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800573 const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
574{
575 // The D-Bus property does not indicate which bucket is the "high
576 // priority" group, so let's discern that by looking for the one with
577 // highest base frequency.
578 auto highPriorityGroup = baseSpeedSettings.cend();
579 uint32_t highestBaseSpeed = 0;
580 for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
581 ++it)
582 {
583 const uint32_t baseFreq = std::get<uint32_t>(*it);
584 if (baseFreq > highestBaseSpeed)
585 {
586 highestBaseSpeed = baseFreq;
587 highPriorityGroup = it;
588 }
589 }
590
Ed Tanousac106bf2023-06-07 09:24:59 -0700591 nlohmann::json& jsonCoreIds = asyncResp->res.jsonValue["HighSpeedCoreIDs"];
Jonathan Domandba0c292020-12-02 15:34:13 -0800592 jsonCoreIds = nlohmann::json::array();
593
594 // There may not be any entries in the D-Bus property, so only populate
595 // if there was actually something there.
596 if (highPriorityGroup != baseSpeedSettings.cend())
597 {
598 jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
599 }
600}
601
602/**
603 * Fill out OperatingConfig related items in a Processor resource by requesting
604 * data from the given D-Bus object.
605 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700606 * @param[in,out] asyncResp Async HTTP response.
Jonathan Domandba0c292020-12-02 15:34:13 -0800607 * @param[in] cpuId CPU D-Bus name.
608 * @param[in] service D-Bus service to query.
609 * @param[in] objPath D-Bus object to query.
610 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700611inline void
612 getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
613 const std::string& cpuId, const std::string& service,
614 const std::string& objPath)
Jonathan Domandba0c292020-12-02 15:34:13 -0800615{
Ed Tanous62598e32023-07-17 17:06:25 -0700616 BMCWEB_LOG_INFO("Getting CPU operating configs for {}", cpuId);
Jonathan Domandba0c292020-12-02 15:34:13 -0800617
618 // First, GetAll CurrentOperatingConfig properties on the object
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200619 sdbusplus::asio::getAllProperties(
620 *crow::connections::systemBus, service, objPath,
621 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Ed Tanousac106bf2023-06-07 09:24:59 -0700622 [asyncResp, cpuId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800623 service](const boost::system::error_code& ec,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200624 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700625 if (ec)
626 {
Ed Tanous62598e32023-07-17 17:06:25 -0700627 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
Ed Tanousac106bf2023-06-07 09:24:59 -0700628 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700629 return;
630 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800631
Ed Tanousac106bf2023-06-07 09:24:59 -0700632 nlohmann::json& json = asyncResp->res.jsonValue;
Jonathan Domandba0c292020-12-02 15:34:13 -0800633
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200634 const sdbusplus::message::object_path* appliedConfig = nullptr;
635 const bool* baseSpeedPriorityEnabled = nullptr;
636
637 const bool success = sdbusplus::unpackPropertiesNoThrow(
638 dbus_utils::UnpackErrorPrinter(), properties, "AppliedConfig",
639 appliedConfig, "BaseSpeedPriorityEnabled",
640 baseSpeedPriorityEnabled);
641
642 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700643 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700644 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200645 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700646 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200647
648 if (appliedConfig != nullptr)
649 {
650 const std::string& dbusPath = appliedConfig->str;
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200651 nlohmann::json::object_t operatingConfig;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700652 operatingConfig["@odata.id"] = boost::urls::format(
653 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs",
654 cpuId);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200655 json["OperatingConfigs"] = std::move(operatingConfig);
656
657 // Reuse the D-Bus config object name for the Redfish
658 // URI
659 size_t baseNamePos = dbusPath.rfind('/');
660 if (baseNamePos == std::string::npos ||
661 baseNamePos == (dbusPath.size() - 1))
662 {
663 // If the AppliedConfig was somehow not a valid path,
664 // skip adding any more properties, since everything
665 // else is tied to this applied config.
Ed Tanousac106bf2023-06-07 09:24:59 -0700666 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200667 return;
668 }
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200669 nlohmann::json::object_t appliedOperatingConfig;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700670 appliedOperatingConfig["@odata.id"] = boost::urls::format(
671 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs/{}",
672 cpuId, dbusPath.substr(baseNamePos + 1));
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200673 json["AppliedOperatingConfig"] = std::move(appliedOperatingConfig);
674
675 // Once we found the current applied config, queue another
676 // request to read the base freq core ids out of that
677 // config.
678 sdbusplus::asio::getProperty<BaseSpeedPrioritySettingsProperty>(
679 *crow::connections::systemBus, service, dbusPath,
680 "xyz.openbmc_project.Inventory.Item.Cpu."
681 "OperatingConfig",
682 "BaseSpeedPrioritySettings",
Ed Tanousac106bf2023-06-07 09:24:59 -0700683 [asyncResp](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800684 const boost::system::error_code& ec2,
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200685 const BaseSpeedPrioritySettingsProperty& baseSpeedList) {
686 if (ec2)
687 {
Ed Tanous62598e32023-07-17 17:06:25 -0700688 BMCWEB_LOG_WARNING("D-Bus Property Get error: {}", ec2);
Ed Tanousac106bf2023-06-07 09:24:59 -0700689 messages::internalError(asyncResp->res);
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200690 return;
691 }
692
Ed Tanousac106bf2023-06-07 09:24:59 -0700693 highSpeedCoreIdsHandler(asyncResp, baseSpeedList);
Patrick Williams5a39f772023-10-20 11:20:21 -0500694 });
Krzysztof Grobelny351053f2022-07-28 15:44:22 +0200695 }
696
697 if (baseSpeedPriorityEnabled != nullptr)
698 {
699 json["BaseSpeedPriorityState"] =
700 *baseSpeedPriorityEnabled ? "Enabled" : "Disabled";
701 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500702 });
Jonathan Domandba0c292020-12-02 15:34:13 -0800703}
704
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600705/**
706 * @brief Fill out location info of a processor by
707 * requesting data from the given D-Bus object.
708 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700709 * @param[in,out] asyncResp Async HTTP response.
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600710 * @param[in] service D-Bus service to query.
711 * @param[in] objPath D-Bus object to query.
712 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700713inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600714 const std::string& service,
715 const std::string& objPath)
716{
Ed Tanous62598e32023-07-17 17:06:25 -0700717 BMCWEB_LOG_DEBUG("Get Cpu Location Data");
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700718 sdbusplus::asio::getProperty<std::string>(
719 *crow::connections::systemBus, service, objPath,
720 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -0700721 [objPath, asyncResp{std::move(asyncResp)}](
722 const boost::system::error_code& ec, const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700723 if (ec)
724 {
Ed Tanous62598e32023-07-17 17:06:25 -0700725 BMCWEB_LOG_DEBUG("DBUS response error");
Ed Tanousac106bf2023-06-07 09:24:59 -0700726 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700727 return;
728 }
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600729
Ed Tanousac106bf2023-06-07 09:24:59 -0700730 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -0700731 property;
Patrick Williams5a39f772023-10-20 11:20:21 -0500732 });
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600733}
734
Jonathan Domanc9514482021-02-24 09:20:51 -0800735/**
Jonathan Doman49e429c2021-03-03 13:11:44 -0800736 * Populate the unique identifier in a Processor resource by requesting data
737 * from the given D-Bus object.
738 *
Ed Tanousac106bf2023-06-07 09:24:59 -0700739 * @param[in,out] asyncResp Async HTTP response.
Jonathan Doman49e429c2021-03-03 13:11:44 -0800740 * @param[in] service D-Bus service to query.
741 * @param[in] objPath D-Bus object to query.
742 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700743inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Jonathan Doman49e429c2021-03-03 13:11:44 -0800744 const std::string& service,
745 const std::string& objectPath)
746{
Ed Tanous62598e32023-07-17 17:06:25 -0700747 BMCWEB_LOG_DEBUG("Get CPU UniqueIdentifier");
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700748 sdbusplus::asio::getProperty<std::string>(
749 *crow::connections::systemBus, service, objectPath,
750 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
751 "UniqueIdentifier",
Ed Tanousac106bf2023-06-07 09:24:59 -0700752 [asyncResp](const boost::system::error_code& ec,
753 const std::string& id) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700754 if (ec)
755 {
Ed Tanous62598e32023-07-17 17:06:25 -0700756 BMCWEB_LOG_ERROR("Failed to read cpu unique id: {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -0700757 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700758 return;
759 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700760 asyncResp->res
761 .jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] = id;
Patrick Williams5a39f772023-10-20 11:20:21 -0500762 });
Jonathan Doman49e429c2021-03-03 13:11:44 -0800763}
764
765/**
Jonathan Domanc9514482021-02-24 09:20:51 -0800766 * Find the D-Bus object representing the requested Processor, and call the
767 * handler with the results. If matching object is not found, add 404 error to
768 * response and don't call the handler.
769 *
770 * @param[in,out] resp Async HTTP response.
771 * @param[in] processorId Redfish Processor Id.
772 * @param[in] handler Callback to continue processing request upon
773 * successfully finding object.
774 */
775template <typename Handler>
zhanghch058d1b46d2021-04-01 11:18:24 +0800776inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800777 const std::string& processorId,
778 Handler&& handler)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500779{
Ed Tanous62598e32023-07-17 17:06:25 -0700780 BMCWEB_LOG_DEBUG("Get available system processor resources.");
Gunnar Millsac6a4442020-10-14 14:55:29 -0500781
Jonathan Domanc9514482021-02-24 09:20:51 -0800782 // GetSubTree on all interfaces which provide info about a Processor
Chris Caindfbf7de2023-04-13 16:01:04 -0500783 constexpr std::array<std::string_view, 9> interfaces = {
George Liue99073f2022-12-09 11:06:16 +0800784 "xyz.openbmc_project.Common.UUID",
785 "xyz.openbmc_project.Inventory.Decorator.Asset",
786 "xyz.openbmc_project.Inventory.Decorator.Revision",
787 "xyz.openbmc_project.Inventory.Item.Cpu",
788 "xyz.openbmc_project.Inventory.Decorator.LocationCode",
789 "xyz.openbmc_project.Inventory.Item.Accelerator",
790 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Chris Caindfbf7de2023-04-13 16:01:04 -0500791 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
792 "xyz.openbmc_project.Control.Power.Throttle"};
George Liue99073f2022-12-09 11:06:16 +0800793 dbus::utility::getSubTree(
794 "/xyz/openbmc_project/inventory", 0, interfaces,
Jonathan Domanc9514482021-02-24 09:20:51 -0800795 [resp, processorId, handler = std::forward<Handler>(handler)](
George Liue99073f2022-12-09 11:06:16 +0800796 const boost::system::error_code& ec,
797 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700798 if (ec)
799 {
Ed Tanous62598e32023-07-17 17:06:25 -0700800 BMCWEB_LOG_DEBUG("DBUS response error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700801 messages::internalError(resp->res);
802 return;
803 }
804 for (const auto& [objectPath, serviceMap] : subtree)
805 {
806 // Ignore any objects which don't end with our desired cpu name
Ed Tanous11ba3972022-07-11 09:50:41 -0700807 if (!objectPath.ends_with(processorId))
Gunnar Millsac6a4442020-10-14 14:55:29 -0500808 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700809 continue;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500810 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700811
812 bool found = false;
813 // Filter out objects that don't have the CPU-specific
814 // interfaces to make sure we can return 404 on non-CPUs
815 // (e.g. /redfish/../Processors/dimm0)
816 for (const auto& [serviceName, interfaceList] : serviceMap)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500817 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700818 if (std::ranges::find_first_of(interfaceList,
819 processorInterfaces) !=
820 std::end(interfaceList))
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);
Patrick Williams5a39f772023-10-20 11:20:21 -0500841 });
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 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001004 });
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 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001086 if (std::ranges::find(interfaceList,
1087 "xyz.openbmc_project.Control.Processor."
1088 "CurrentOperatingConfig") != interfaceList.end())
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001089 {
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);
Patrick Williams5a39f772023-10-20 11:20:21 -05001131 });
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.
Patrick Williams5a39f772023-10-20 11:20:21 -05001227 constexpr std::array<std::string_view, 1> interface{
1228 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"};
Ed Tanous002d39b2022-05-31 08:59:27 -07001229 collection_util::getCollectionMembers(
1230 asyncResp,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001231 boost::urls::format(
1232 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs",
1233 cpuName),
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -05001234 interface, object);
Ed Tanous002d39b2022-05-31 08:59:27 -07001235 return;
1236 }
George Liu0fda0f12021-11-16 10:06:17 +08001237 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001238 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001239}
1240
1241inline void requestRoutesOperatingConfig(App& app)
1242{
1243 BMCWEB_ROUTE(
1244 app,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001245 "/redfish/v1/Systems/<str>/Processors/<str>/OperatingConfigs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001246 .privileges(redfish::privileges::getOperatingConfig)
Ed Tanous002d39b2022-05-31 08:59:27 -07001247 .methods(boost::beast::http::verb::get)(
1248 [&app](const crow::Request& req,
1249 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001250 const std::string& systemName, const std::string& cpuName,
1251 const std::string& configName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001252 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001253 {
1254 return;
1255 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001256 if constexpr (bmcwebEnableMultiHost)
1257 {
1258 // Option currently returns no systems. TBD
1259 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1260 systemName);
1261 return;
1262 }
1263
1264 if (systemName != "system")
1265 {
1266 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1267 systemName);
1268 return;
1269 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001270 // Ask for all objects implementing OperatingConfig so we can search
1271 // for one with a matching name
George Liue99073f2022-12-09 11:06:16 +08001272 constexpr std::array<std::string_view, 1> interfaces = {
1273 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"};
1274 dbus::utility::getSubTree(
1275 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous39662a32023-02-06 15:09:46 -08001276 [asyncResp, cpuName, configName](
George Liue99073f2022-12-09 11:06:16 +08001277 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001278 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1279 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001280 {
Ed Tanous62598e32023-07-17 17:06:25 -07001281 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07001282 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001283 return;
1284 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001285 const std::string expectedEnding = cpuName + '/' + configName;
1286 for (const auto& [objectPath, serviceMap] : subtree)
1287 {
1288 // Ignore any configs without matching cpuX/configY
Ed Tanous11ba3972022-07-11 09:50:41 -07001289 if (!objectPath.ends_with(expectedEnding) || serviceMap.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -07001290 {
1291 continue;
1292 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001293
Ed Tanous002d39b2022-05-31 08:59:27 -07001294 nlohmann::json& json = asyncResp->res.jsonValue;
1295 json["@odata.type"] = "#OperatingConfig.v1_0_0.OperatingConfig";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001296 json["@odata.id"] = boost::urls::format(
1297 "/redfish/v1/Systems/system/Processors/{}/OperatingConfigs/{}",
1298 cpuName, configName);
Ed Tanous002d39b2022-05-31 08:59:27 -07001299 json["Name"] = "Processor Profile";
1300 json["Id"] = configName;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001301
Ed Tanous002d39b2022-05-31 08:59:27 -07001302 // Just use the first implementation of the object - not
1303 // expected that there would be multiple matching
1304 // services
1305 getOperatingConfigData(asyncResp, serviceMap.begin()->first,
1306 objectPath);
1307 return;
1308 }
1309 messages::resourceNotFound(asyncResp->res, "OperatingConfig",
1310 configName);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001311 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001312 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001313}
Jonathan Domandba0c292020-12-02 15:34:13 -08001314
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001315inline void requestRoutesProcessorCollection(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001316{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001317 /**
1318 * Functions triggers appropriate requests on DBus
1319 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001320 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001321 .privileges(redfish::privileges::headProcessorCollection)
1322 .methods(boost::beast::http::verb::head)(
1323 std::bind_front(handleProcessorCollectionHead, std::ref(app)));
1324
1325 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
Ed Tanoused398212021-06-09 17:05:54 -07001326 .privileges(redfish::privileges::getProcessorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001327 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001328 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001329 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1330 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001331 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001332 {
1333 return;
1334 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001335 if constexpr (bmcwebEnableMultiHost)
1336 {
1337 // Option currently returns no systems. TBD
1338 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1339 systemName);
1340 return;
1341 }
1342
Ed Tanous22d268c2022-05-19 09:39:07 -07001343 if (systemName != "system")
1344 {
1345 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1346 systemName);
1347 return;
1348 }
1349
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001350 asyncResp->res.addHeader(
1351 boost::beast::http::field::link,
1352 "</redfish/v1/JsonSchemas/ProcessorCollection/ProcessorCollection.json>; rel=describedby");
1353
Ed Tanous002d39b2022-05-31 08:59:27 -07001354 asyncResp->res.jsonValue["@odata.type"] =
1355 "#ProcessorCollection.ProcessorCollection";
1356 asyncResp->res.jsonValue["Name"] = "Processor Collection";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001357
Ed Tanous002d39b2022-05-31 08:59:27 -07001358 asyncResp->res.jsonValue["@odata.id"] =
1359 "/redfish/v1/Systems/system/Processors";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001360
Ed Tanous002d39b2022-05-31 08:59:27 -07001361 collection_util::getCollectionMembers(
Willy Tuae9031f2022-09-27 05:48:07 +00001362 asyncResp,
1363 boost::urls::url("/redfish/v1/Systems/system/Processors"),
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -05001364 processorInterfaces, "/xyz/openbmc_project/inventory");
Patrick Williams5a39f772023-10-20 11:20:21 -05001365 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001366}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001367
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001368inline void requestRoutesProcessor(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001369{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001370 /**
1371 * Functions triggers appropriate requests on DBus
1372 */
Gunnar Millsac6a4442020-10-14 14:55:29 -05001373
Ed Tanous22d268c2022-05-19 09:39:07 -07001374 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001375 .privileges(redfish::privileges::headProcessor)
1376 .methods(boost::beast::http::verb::head)(
1377 std::bind_front(handleProcessorHead, std::ref(app)));
1378
1379 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001380 .privileges(redfish::privileges::getProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001381 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001382 [&app](const crow::Request& req,
1383 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001384 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001385 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001386 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001387 {
1388 return;
1389 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001390 if constexpr (bmcwebEnableMultiHost)
1391 {
1392 // Option currently returns no systems. TBD
1393 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1394 systemName);
1395 return;
1396 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001397 if (systemName != "system")
1398 {
1399 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1400 systemName);
1401 return;
1402 }
1403
Nikhil Namjoshi71a24ca2022-11-11 01:52:32 +00001404 asyncResp->res.addHeader(
1405 boost::beast::http::field::link,
1406 "</redfish/v1/JsonSchemas/Processor/Processor.json>; rel=describedby");
Ed Tanous002d39b2022-05-31 08:59:27 -07001407 asyncResp->res.jsonValue["@odata.type"] =
Chris Caindfbf7de2023-04-13 16:01:04 -05001408 "#Processor.v1_18_0.Processor";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001409 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1410 "/redfish/v1/Systems/system/Processors/{}", processorId);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001411
Ed Tanous8a592812022-06-04 09:06:59 -07001412 getProcessorObject(
1413 asyncResp, processorId,
1414 std::bind_front(getProcessorData, asyncResp, processorId));
Patrick Williams5a39f772023-10-20 11:20:21 -05001415 });
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001416
Ed Tanous22d268c2022-05-19 09:39:07 -07001417 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001418 .privileges(redfish::privileges::patchProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001419 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001420 [&app](const crow::Request& req,
1421 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001422 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001423 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001424 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001425 {
1426 return;
1427 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001428 if constexpr (bmcwebEnableMultiHost)
1429 {
1430 // Option currently returns no systems. TBD
1431 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1432 systemName);
1433 return;
1434 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001435 if (systemName != "system")
1436 {
1437 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1438 systemName);
1439 return;
1440 }
1441
Ed Tanous002d39b2022-05-31 08:59:27 -07001442 std::optional<nlohmann::json> appliedConfigJson;
1443 if (!json_util::readJsonPatch(req, asyncResp->res,
1444 "AppliedOperatingConfig",
1445 appliedConfigJson))
1446 {
1447 return;
1448 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001449
Ed Tanous002d39b2022-05-31 08:59:27 -07001450 if (appliedConfigJson)
1451 {
Ed Tanousf8fe53e2022-06-30 15:55:45 -07001452 std::string appliedConfigUri;
Ed Tanous002d39b2022-05-31 08:59:27 -07001453 if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
1454 "@odata.id", appliedConfigUri))
1455 {
1456 return;
1457 }
1458 // Check for 404 and find matching D-Bus object, then run
1459 // property patch handlers if that all succeeds.
Ed Tanous8a592812022-06-04 09:06:59 -07001460 getProcessorObject(asyncResp, processorId,
1461 std::bind_front(patchAppliedOperatingConfig,
1462 asyncResp, processorId,
1463 appliedConfigUri));
Ed Tanous002d39b2022-05-31 08:59:27 -07001464 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001465 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001466}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001467
1468} // namespace redfish