blob: a8cfb68d473177afb1acd5a5a3d545664a23b409 [file] [log] [blame]
Gunnar Millsac6a4442020-10-14 14:55:29 -05001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Jonathan Doman1e1e5982021-06-11 09:36:17 -070018#include "dbus_singleton.hpp"
19#include "error_messages.hpp"
Gunnar Millsac6a4442020-10-14 14:55:29 -050020#include "health.hpp"
21
John Edward Broadbent7e860f12021-04-08 15:57:16 -070022#include <app.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050023#include <boost/container/flat_map.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080024#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070025#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070026#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070027#include <sdbusplus/asio/property.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080028#include <sdbusplus/message/native_types.hpp>
29#include <sdbusplus/utility/dedup_variant.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050030#include <utils/collection.hpp>
31#include <utils/json_utils.hpp>
32
33namespace redfish
34{
35
Jonathan Domanc9514482021-02-24 09:20:51 -080036// Interfaces which imply a D-Bus object represents a Processor
37constexpr std::array<const char*, 2> processorInterfaces = {
38 "xyz.openbmc_project.Inventory.Item.Cpu",
39 "xyz.openbmc_project.Inventory.Item.Accelerator"};
Jonathan Doman2bab9832020-12-02 15:27:40 -080040
Sharad Yadav71b82f22021-05-10 15:11:39 +053041/**
42 * @brief Fill out uuid info of a processor by
43 * requesting data from the given D-Bus object.
44 *
45 * @param[in,out] aResp Async HTTP response.
46 * @param[in] service D-Bus service to query.
47 * @param[in] objPath D-Bus object to query.
48 */
49inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
50 const std::string& service,
51 const std::string& objPath)
52{
53 BMCWEB_LOG_DEBUG << "Get Processor UUID";
Jonathan Doman1e1e5982021-06-11 09:36:17 -070054 sdbusplus::asio::getProperty<std::string>(
55 *crow::connections::systemBus, service, objPath,
56 "xyz.openbmc_project.Common.UUID", "UUID",
57 [objPath, aResp{std::move(aResp)}](const boost::system::error_code ec,
58 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -070059 if (ec)
60 {
61 BMCWEB_LOG_DEBUG << "DBUS response error";
62 messages::internalError(aResp->res);
63 return;
64 }
65 aResp->res.jsonValue["UUID"] = property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -070066 });
Sharad Yadav71b82f22021-05-10 15:11:39 +053067}
68
Ed Tanous711ac7a2021-12-20 09:34:41 -080069inline void getCpuDataByInterface(
70 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
71 const dbus::utility::DBusInteracesMap& cpuInterfacesProperties)
Gunnar Millsac6a4442020-10-14 14:55:29 -050072{
73 BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
74
Chicago Duana1649ec2021-03-30 16:54:58 +080075 // Set the default value of state
76 aResp->res.jsonValue["Status"]["State"] = "Enabled";
77 aResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -050078
79 for (const auto& interface : cpuInterfacesProperties)
80 {
81 for (const auto& property : interface.second)
82 {
Chicago Duana1649ec2021-03-30 16:54:58 +080083 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -050084 {
Chicago Duana1649ec2021-03-30 16:54:58 +080085 const bool* cpuPresent = std::get_if<bool>(&property.second);
86 if (cpuPresent == nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -050087 {
88 // Important property not in desired type
89 messages::internalError(aResp->res);
90 return;
91 }
Ed Tanouse05aec52022-01-25 10:28:56 -080092 if (!*cpuPresent)
Gunnar Millsac6a4442020-10-14 14:55:29 -050093 {
Chicago Duana1649ec2021-03-30 16:54:58 +080094 // Slot is not populated
Gunnar Millsac6a4442020-10-14 14:55:29 -050095 aResp->res.jsonValue["Status"]["State"] = "Absent";
Chicago Duana1649ec2021-03-30 16:54:58 +080096 }
97 }
98 else if (property.first == "Functional")
99 {
100 const bool* cpuFunctional = std::get_if<bool>(&property.second);
101 if (cpuFunctional == nullptr)
102 {
103 messages::internalError(aResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500104 return;
105 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800106 if (!*cpuFunctional)
Chicago Duana1649ec2021-03-30 16:54:58 +0800107 {
108 aResp->res.jsonValue["Status"]["Health"] = "Critical";
109 }
110 }
111 else if (property.first == "CoreCount")
112 {
113 const uint16_t* coresCount =
114 std::get_if<uint16_t>(&property.second);
115 if (coresCount == nullptr)
116 {
117 messages::internalError(aResp->res);
118 return;
119 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500120 aResp->res.jsonValue["TotalCores"] = *coresCount;
121 }
Jonathan Domandc3fa662020-10-26 23:10:24 -0700122 else if (property.first == "MaxSpeedInMhz")
123 {
124 const uint32_t* value = std::get_if<uint32_t>(&property.second);
125 if (value != nullptr)
126 {
127 aResp->res.jsonValue["MaxSpeedMHz"] = *value;
128 }
129 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500130 else if (property.first == "Socket")
131 {
132 const std::string* value =
133 std::get_if<std::string>(&property.second);
134 if (value != nullptr)
135 {
136 aResp->res.jsonValue["Socket"] = *value;
137 }
138 }
139 else if (property.first == "ThreadCount")
140 {
Jonathan Domandc3fa662020-10-26 23:10:24 -0700141 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500142 if (value != nullptr)
143 {
144 aResp->res.jsonValue["TotalThreads"] = *value;
145 }
146 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700147 else if (property.first == "EffectiveFamily")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500148 {
Brandon Kim1930fbd2021-09-14 17:52:51 -0700149 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500150 if (value != nullptr)
151 {
152 aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800153 "0x" + intToHexString(*value, 4);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500154 }
155 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700156 else if (property.first == "EffectiveModel")
157 {
158 const uint16_t* value = std::get_if<uint16_t>(&property.second);
159 if (value == nullptr)
160 {
161 messages::internalError(aResp->res);
162 return;
163 }
164 aResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800165 "0x" + intToHexString(*value, 4);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700166 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500167 else if (property.first == "Id")
168 {
169 const uint64_t* value = std::get_if<uint64_t>(&property.second);
170 if (value != nullptr && *value != 0)
171 {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500172 aResp->res
173 .jsonValue["ProcessorId"]["IdentificationRegisters"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800174 "0x" + intToHexString(*value, 16);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500175 }
176 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700177 else if (property.first == "Microcode")
178 {
179 const uint32_t* value = std::get_if<uint32_t>(&property.second);
180 if (value == nullptr)
181 {
182 messages::internalError(aResp->res);
183 return;
184 }
185 aResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800186 "0x" + intToHexString(*value, 8);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700187 }
188 else if (property.first == "Step")
189 {
190 const uint16_t* value = std::get_if<uint16_t>(&property.second);
191 if (value == nullptr)
192 {
193 messages::internalError(aResp->res);
194 return;
195 }
196 aResp->res.jsonValue["ProcessorId"]["Step"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800197 "0x" + intToHexString(*value, 4);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700198 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500199 }
200 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500201}
202
zhanghch058d1b46d2021-04-01 11:18:24 +0800203inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500204 const std::string& cpuId,
205 const std::string& service,
206 const std::string& objPath)
207{
208 BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
209
210 crow::connections::systemBus->async_method_call(
211 [cpuId, service, objPath, aResp{std::move(aResp)}](
212 const boost::system::error_code ec,
213 const dbus::utility::ManagedObjectType& dbusData) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700214 if (ec)
215 {
216 BMCWEB_LOG_DEBUG << "DBUS response error";
217 messages::internalError(aResp->res);
218 return;
219 }
220 aResp->res.jsonValue["Id"] = cpuId;
221 aResp->res.jsonValue["Name"] = "Processor";
222 aResp->res.jsonValue["ProcessorType"] = "CPU";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500223
Ed Tanous002d39b2022-05-31 08:59:27 -0700224 bool slotPresent = false;
225 std::string corePath = objPath + "/core";
226 size_t totalCores = 0;
227 for (const auto& object : dbusData)
228 {
229 if (object.first.str == objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500230 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700231 getCpuDataByInterface(aResp, object.second);
232 }
233 else if (boost::starts_with(object.first.str, corePath))
234 {
235 for (const auto& interface : object.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500236 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700237 if (interface.first == "xyz.openbmc_project.Inventory.Item")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500238 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700239 for (const auto& property : interface.second)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500240 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700241 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500242 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700243 const bool* present =
244 std::get_if<bool>(&property.second);
245 if (present != nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500246 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700247 if (*present)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500248 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700249 slotPresent = true;
250 totalCores++;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500251 }
252 }
253 }
254 }
255 }
256 }
257 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700258 }
259 // In getCpuDataByInterface(), state and health are set
260 // based on the present and functional status. If core
261 // count is zero, then it has a higher precedence.
262 if (slotPresent)
263 {
264 if (totalCores == 0)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500265 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700266 // Slot is not populated, set status end return
267 aResp->res.jsonValue["Status"]["State"] = "Absent";
268 aResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500269 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700270 aResp->res.jsonValue["TotalCores"] = totalCores;
271 }
272 return;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500273 },
274 service, "/xyz/openbmc_project/inventory",
275 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
276}
277
zhanghch058d1b46d2021-04-01 11:18:24 +0800278inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500279 const std::string& service,
280 const std::string& objPath)
281{
282 BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
283 crow::connections::systemBus->async_method_call(
284 [objPath, aResp{std::move(aResp)}](
285 const boost::system::error_code ec,
286 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800287 std::string, dbus::utility::DbusVariantType>& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700288 if (ec)
289 {
290 BMCWEB_LOG_DEBUG << "DBUS response error";
291 messages::internalError(aResp->res);
292 return;
293 }
294
295 for (const auto& property : properties)
296 {
297 if (property.first == "SerialNumber")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500298 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700299 const std::string* sn =
300 std::get_if<std::string>(&property.second);
301 if (sn != nullptr && !sn->empty())
Gunnar Millsac6a4442020-10-14 14:55:29 -0500302 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700303 aResp->res.jsonValue["SerialNumber"] = *sn;
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600304 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500305 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700306 else if (property.first == "Model")
307 {
308 const std::string* model =
309 std::get_if<std::string>(&property.second);
310 if (model != nullptr && !model->empty())
311 {
312 aResp->res.jsonValue["Model"] = *model;
313 }
314 }
315 else if (property.first == "Manufacturer")
316 {
317
318 const std::string* mfg =
319 std::get_if<std::string>(&property.second);
320 if (mfg != nullptr)
321 {
322 aResp->res.jsonValue["Manufacturer"] = *mfg;
323
324 // Otherwise would be unexpected.
325 if (mfg->find("Intel") != std::string::npos)
326 {
327 aResp->res.jsonValue["ProcessorArchitecture"] = "x86";
328 aResp->res.jsonValue["InstructionSet"] = "x86-64";
329 }
330 else if (mfg->find("IBM") != std::string::npos)
331 {
332 aResp->res.jsonValue["ProcessorArchitecture"] = "Power";
333 aResp->res.jsonValue["InstructionSet"] = "PowerISA";
334 }
335 }
336 }
337 else if (property.first == "PartNumber")
338 {
339 const std::string* partNumber =
340 std::get_if<std::string>(&property.second);
341
342 if (partNumber == nullptr)
343 {
344 messages::internalError(aResp->res);
345 return;
346 }
347 aResp->res.jsonValue["PartNumber"] = *partNumber;
348 }
349 else if (property.first == "SparePartNumber")
350 {
351 const std::string* sparePartNumber =
352 std::get_if<std::string>(&property.second);
353
354 if (sparePartNumber == nullptr)
355 {
356 messages::internalError(aResp->res);
357 return;
358 }
359 aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
360 }
361 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500362 },
363 service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
364 "xyz.openbmc_project.Inventory.Decorator.Asset");
365}
366
zhanghch058d1b46d2021-04-01 11:18:24 +0800367inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500368 const std::string& service,
369 const std::string& objPath)
370{
371 BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
372 crow::connections::systemBus->async_method_call(
373 [objPath, aResp{std::move(aResp)}](
374 const boost::system::error_code ec,
375 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800376 std::string, dbus::utility::DbusVariantType>& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700377 if (ec)
378 {
379 BMCWEB_LOG_DEBUG << "DBUS response error";
380 messages::internalError(aResp->res);
381 return;
382 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500383
Ed Tanous002d39b2022-05-31 08:59:27 -0700384 for (const auto& property : properties)
385 {
386 if (property.first == "Version")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500387 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700388 const std::string* ver =
389 std::get_if<std::string>(&property.second);
390 if (ver != nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500391 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700392 aResp->res.jsonValue["Version"] = *ver;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500393 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700394 break;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500395 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700396 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500397 },
398 service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
399 "xyz.openbmc_project.Inventory.Decorator.Revision");
400}
401
zhanghch058d1b46d2021-04-01 11:18:24 +0800402inline void getAcceleratorDataByService(
403 std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
404 const std::string& service, const std::string& objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500405{
406 BMCWEB_LOG_DEBUG
407 << "Get available system Accelerator resources by service.";
408 crow::connections::systemBus->async_method_call(
409 [acclrtrId, aResp{std::move(aResp)}](
410 const boost::system::error_code ec,
411 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800412 std::string, dbus::utility::DbusVariantType>& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700413 if (ec)
414 {
415 BMCWEB_LOG_DEBUG << "DBUS response error";
416 messages::internalError(aResp->res);
417 return;
418 }
419 aResp->res.jsonValue["Id"] = acclrtrId;
420 aResp->res.jsonValue["Name"] = "Processor";
421 const bool* accPresent = nullptr;
422 const bool* accFunctional = nullptr;
423
424 for (const auto& property : properties)
425 {
426 if (property.first == "Functional")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500427 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700428 accFunctional = std::get_if<bool>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500429 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700430 else if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500431 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700432 accPresent = std::get_if<bool>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500433 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700434 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500435
Ed Tanous002d39b2022-05-31 08:59:27 -0700436 std::string state = "Enabled";
437 std::string health = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500438
Ed Tanous002d39b2022-05-31 08:59:27 -0700439 if (accPresent != nullptr && !*accPresent)
440 {
441 state = "Absent";
442 }
443
444 if ((accFunctional != nullptr) && !*accFunctional)
445 {
446 if (state == "Enabled")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500447 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700448 health = "Critical";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500449 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700450 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500451
Ed Tanous002d39b2022-05-31 08:59:27 -0700452 aResp->res.jsonValue["Status"]["State"] = state;
453 aResp->res.jsonValue["Status"]["Health"] = health;
454 aResp->res.jsonValue["ProcessorType"] = "Accelerator";
Gunnar Millsac6a4442020-10-14 14:55:29 -0500455 },
456 service, objPath, "org.freedesktop.DBus.Properties", "GetAll", "");
457}
458
Jonathan Domandba0c292020-12-02 15:34:13 -0800459// OperatingConfig D-Bus Types
460using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
461using BaseSpeedPrioritySettingsProperty =
462 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
463// uint32_t and size_t may or may not be the same type, requiring a dedup'd
464// variant
Ed Tanous168e20c2021-12-13 14:39:53 -0800465using OperatingConfigProperties =
466 std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>;
Jonathan Domandba0c292020-12-02 15:34:13 -0800467
468/**
469 * Fill out the HighSpeedCoreIDs in a Processor resource from the given
470 * OperatingConfig D-Bus property.
471 *
472 * @param[in,out] aResp Async HTTP response.
473 * @param[in] baseSpeedSettings Full list of base speed priority groups,
474 * to use to determine the list of high
475 * speed cores.
476 */
477inline void highSpeedCoreIdsHandler(
zhanghch058d1b46d2021-04-01 11:18:24 +0800478 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800479 const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
480{
481 // The D-Bus property does not indicate which bucket is the "high
482 // priority" group, so let's discern that by looking for the one with
483 // highest base frequency.
484 auto highPriorityGroup = baseSpeedSettings.cend();
485 uint32_t highestBaseSpeed = 0;
486 for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
487 ++it)
488 {
489 const uint32_t baseFreq = std::get<uint32_t>(*it);
490 if (baseFreq > highestBaseSpeed)
491 {
492 highestBaseSpeed = baseFreq;
493 highPriorityGroup = it;
494 }
495 }
496
497 nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
498 jsonCoreIds = nlohmann::json::array();
499
500 // There may not be any entries in the D-Bus property, so only populate
501 // if there was actually something there.
502 if (highPriorityGroup != baseSpeedSettings.cend())
503 {
504 jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
505 }
506}
507
508/**
509 * Fill out OperatingConfig related items in a Processor resource by requesting
510 * data from the given D-Bus object.
511 *
512 * @param[in,out] aResp Async HTTP response.
513 * @param[in] cpuId CPU D-Bus name.
514 * @param[in] service D-Bus service to query.
515 * @param[in] objPath D-Bus object to query.
516 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800517inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800518 const std::string& cpuId,
519 const std::string& service,
520 const std::string& objPath)
521{
522 BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
523
524 // First, GetAll CurrentOperatingConfig properties on the object
525 crow::connections::systemBus->async_method_call(
526 [aResp, cpuId, service](
527 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -0800528 const std::vector<std::pair<
529 std::string, dbus::utility::DbusVariantType>>& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700530 if (ec)
531 {
532 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
533 messages::internalError(aResp->res);
534 return;
535 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800536
Ed Tanous002d39b2022-05-31 08:59:27 -0700537 nlohmann::json& json = aResp->res.jsonValue;
Jonathan Domandba0c292020-12-02 15:34:13 -0800538
Ed Tanous002d39b2022-05-31 08:59:27 -0700539 for (const auto& [dbusPropName, variantVal] : properties)
540 {
541 if (dbusPropName == "AppliedConfig")
Jonathan Domandba0c292020-12-02 15:34:13 -0800542 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 const sdbusplus::message::object_path* dbusPathWrapper =
544 std::get_if<sdbusplus::message::object_path>(&variantVal);
545 if (dbusPathWrapper == nullptr)
Jonathan Domandba0c292020-12-02 15:34:13 -0800546 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700547 continue;
548 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800549
Ed Tanous002d39b2022-05-31 08:59:27 -0700550 const std::string& dbusPath = dbusPathWrapper->str;
551 std::string uri = "/redfish/v1/Systems/system/Processors/" +
552 cpuId + "/OperatingConfigs";
553 nlohmann::json::object_t operatingConfig;
554 operatingConfig["@odata.id"] = uri;
555 json["OperatingConfigs"] = std::move(operatingConfig);
Jonathan Domandba0c292020-12-02 15:34:13 -0800556
Ed Tanous002d39b2022-05-31 08:59:27 -0700557 // Reuse the D-Bus config object name for the Redfish
558 // URI
559 size_t baseNamePos = dbusPath.rfind('/');
560 if (baseNamePos == std::string::npos ||
561 baseNamePos == (dbusPath.size() - 1))
562 {
563 // If the AppliedConfig was somehow not a valid path,
564 // skip adding any more properties, since everything
565 // else is tied to this applied config.
566 messages::internalError(aResp->res);
567 break;
568 }
569 uri += '/';
570 uri += dbusPath.substr(baseNamePos + 1);
571 nlohmann::json::object_t appliedOperatingConfig;
572 appliedOperatingConfig["@odata.id"] = uri;
573 json["AppliedOperatingConfig"] =
574 std::move(appliedOperatingConfig);
575
576 // Once we found the current applied config, queue another
577 // request to read the base freq core ids out of that
578 // config.
579 sdbusplus::asio::getProperty<BaseSpeedPrioritySettingsProperty>(
580 *crow::connections::systemBus, service, dbusPath,
581 "xyz.openbmc_project.Inventory.Item.Cpu."
582 "OperatingConfig",
583 "BaseSpeedPrioritySettings",
Ed Tanous8a592812022-06-04 09:06:59 -0700584 [aResp](const boost::system::error_code ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -0700585 const BaseSpeedPrioritySettingsProperty&
586 baseSpeedList) {
Ed Tanous8a592812022-06-04 09:06:59 -0700587 if (ec2)
Jonathan Domandba0c292020-12-02 15:34:13 -0800588 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 BMCWEB_LOG_WARNING << "D-Bus Property Get error: "
Ed Tanous8a592812022-06-04 09:06:59 -0700590 << ec2;
Jonathan Domandba0c292020-12-02 15:34:13 -0800591 messages::internalError(aResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 return;
Jonathan Domandba0c292020-12-02 15:34:13 -0800593 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800594
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 highSpeedCoreIdsHandler(aResp, baseSpeedList);
596 });
597 }
598 else if (dbusPropName == "BaseSpeedPriorityEnabled")
599 {
600 const bool* state = std::get_if<bool>(&variantVal);
601 if (state != nullptr)
Jonathan Domandba0c292020-12-02 15:34:13 -0800602 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700603 json["BaseSpeedPriorityState"] =
604 *state ? "Enabled" : "Disabled";
Jonathan Domandba0c292020-12-02 15:34:13 -0800605 }
606 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700607 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800608 },
609 service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
610 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig");
611}
612
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600613/**
614 * @brief Fill out location info of a processor by
615 * requesting data from the given D-Bus object.
616 *
617 * @param[in,out] aResp Async HTTP response.
618 * @param[in] service D-Bus service to query.
619 * @param[in] objPath D-Bus object to query.
620 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800621inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> aResp,
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600622 const std::string& service,
623 const std::string& objPath)
624{
625 BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700626 sdbusplus::asio::getProperty<std::string>(
627 *crow::connections::systemBus, service, objPath,
628 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
629 [objPath, aResp{std::move(aResp)}](const boost::system::error_code ec,
630 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700631 if (ec)
632 {
633 BMCWEB_LOG_DEBUG << "DBUS response error";
634 messages::internalError(aResp->res);
635 return;
636 }
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600637
Ed Tanous002d39b2022-05-31 08:59:27 -0700638 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
639 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700640 });
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600641}
642
Jonathan Domanc9514482021-02-24 09:20:51 -0800643/**
Jonathan Doman49e429c2021-03-03 13:11:44 -0800644 * Populate the unique identifier in a Processor resource by requesting data
645 * from the given D-Bus object.
646 *
647 * @param[in,out] aResp Async HTTP response.
648 * @param[in] service D-Bus service to query.
649 * @param[in] objPath D-Bus object to query.
650 */
651inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
652 const std::string& service,
653 const std::string& objectPath)
654{
655 BMCWEB_LOG_DEBUG << "Get CPU UniqueIdentifier";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700656 sdbusplus::asio::getProperty<std::string>(
657 *crow::connections::systemBus, service, objectPath,
658 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
659 "UniqueIdentifier",
660 [aResp](boost::system::error_code ec, const std::string& id) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700661 if (ec)
662 {
663 BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
664 messages::internalError(aResp->res);
665 return;
666 }
667 aResp->res.jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] =
668 id;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700669 });
Jonathan Doman49e429c2021-03-03 13:11:44 -0800670}
671
672/**
Jonathan Domanc9514482021-02-24 09:20:51 -0800673 * Find the D-Bus object representing the requested Processor, and call the
674 * handler with the results. If matching object is not found, add 404 error to
675 * response and don't call the handler.
676 *
677 * @param[in,out] resp Async HTTP response.
678 * @param[in] processorId Redfish Processor Id.
679 * @param[in] handler Callback to continue processing request upon
680 * successfully finding object.
681 */
682template <typename Handler>
zhanghch058d1b46d2021-04-01 11:18:24 +0800683inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800684 const std::string& processorId,
685 Handler&& handler)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500686{
687 BMCWEB_LOG_DEBUG << "Get available system processor resources.";
688
Jonathan Domanc9514482021-02-24 09:20:51 -0800689 // GetSubTree on all interfaces which provide info about a Processor
Gunnar Millsac6a4442020-10-14 14:55:29 -0500690 crow::connections::systemBus->async_method_call(
Jonathan Domanc9514482021-02-24 09:20:51 -0800691 [resp, processorId, handler = std::forward<Handler>(handler)](
692 boost::system::error_code ec,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600693 const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700694 if (ec)
695 {
696 BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
697 messages::internalError(resp->res);
698 return;
699 }
700 for (const auto& [objectPath, serviceMap] : subtree)
701 {
702 // Ignore any objects which don't end with our desired cpu name
703 if (!boost::ends_with(objectPath, processorId))
Gunnar Millsac6a4442020-10-14 14:55:29 -0500704 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700705 continue;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500706 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700707
708 bool found = false;
709 // Filter out objects that don't have the CPU-specific
710 // interfaces to make sure we can return 404 on non-CPUs
711 // (e.g. /redfish/../Processors/dimm0)
712 for (const auto& [serviceName, interfaceList] : serviceMap)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500713 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700714 if (std::find_first_of(
715 interfaceList.begin(), interfaceList.end(),
716 processorInterfaces.begin(),
717 processorInterfaces.end()) != interfaceList.end())
Gunnar Millsac6a4442020-10-14 14:55:29 -0500718 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700719 found = true;
720 break;
Jonathan Doman2bab9832020-12-02 15:27:40 -0800721 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500722 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700723
724 if (!found)
725 {
726 continue;
727 }
728
729 // Process the first object which does match our cpu name and
730 // required interfaces, and potentially ignore any other
731 // matching objects. Assume all interfaces we want to process
732 // must be on the same object path.
733
Ed Tanous8a592812022-06-04 09:06:59 -0700734 handler(objectPath, serviceMap);
Ed Tanous002d39b2022-05-31 08:59:27 -0700735 return;
736 }
737 messages::resourceNotFound(resp->res, "Processor", processorId);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500738 },
739 "xyz.openbmc_project.ObjectMapper",
740 "/xyz/openbmc_project/object_mapper",
741 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
Jonathan Doman2bab9832020-12-02 15:27:40 -0800742 "/xyz/openbmc_project/inventory", 0,
Jonathan Doman49e429c2021-03-03 13:11:44 -0800743 std::array<const char*, 8>{
Sharad Yadav71b82f22021-05-10 15:11:39 +0530744 "xyz.openbmc_project.Common.UUID",
Jonathan Doman2bab9832020-12-02 15:27:40 -0800745 "xyz.openbmc_project.Inventory.Decorator.Asset",
746 "xyz.openbmc_project.Inventory.Decorator.Revision",
747 "xyz.openbmc_project.Inventory.Item.Cpu",
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600748 "xyz.openbmc_project.Inventory.Decorator.LocationCode",
Jonathan Domandba0c292020-12-02 15:34:13 -0800749 "xyz.openbmc_project.Inventory.Item.Accelerator",
Jonathan Doman49e429c2021-03-03 13:11:44 -0800750 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
751 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier"});
Gunnar Millsac6a4442020-10-14 14:55:29 -0500752}
753
zhanghch058d1b46d2021-04-01 11:18:24 +0800754inline void getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domanc9514482021-02-24 09:20:51 -0800755 const std::string& processorId,
756 const std::string& objectPath,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600757 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Domanc9514482021-02-24 09:20:51 -0800758{
759 for (const auto& [serviceName, interfaceList] : serviceMap)
760 {
761 for (const auto& interface : interfaceList)
762 {
763 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
764 {
765 getCpuAssetData(aResp, serviceName, objectPath);
766 }
George Liu0fda0f12021-11-16 10:06:17 +0800767 else if (interface ==
768 "xyz.openbmc_project.Inventory.Decorator.Revision")
Jonathan Domanc9514482021-02-24 09:20:51 -0800769 {
770 getCpuRevisionData(aResp, serviceName, objectPath);
771 }
772 else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
773 {
774 getCpuDataByService(aResp, processorId, serviceName,
775 objectPath);
776 }
George Liu0fda0f12021-11-16 10:06:17 +0800777 else if (interface ==
778 "xyz.openbmc_project.Inventory.Item.Accelerator")
Jonathan Domanc9514482021-02-24 09:20:51 -0800779 {
780 getAcceleratorDataByService(aResp, processorId, serviceName,
781 objectPath);
782 }
George Liu0fda0f12021-11-16 10:06:17 +0800783 else if (
784 interface ==
785 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig")
Jonathan Domanc9514482021-02-24 09:20:51 -0800786 {
787 getCpuConfigData(aResp, processorId, serviceName, objectPath);
788 }
George Liu0fda0f12021-11-16 10:06:17 +0800789 else if (interface ==
790 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
Jonathan Domanc9514482021-02-24 09:20:51 -0800791 {
792 getCpuLocationCode(aResp, serviceName, objectPath);
793 }
Sharad Yadav71b82f22021-05-10 15:11:39 +0530794 else if (interface == "xyz.openbmc_project.Common.UUID")
795 {
796 getProcessorUUID(aResp, serviceName, objectPath);
797 }
George Liu0fda0f12021-11-16 10:06:17 +0800798 else if (interface ==
799 "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier")
Jonathan Doman49e429c2021-03-03 13:11:44 -0800800 {
801 getCpuUniqueId(aResp, serviceName, objectPath);
802 }
Jonathan Domanc9514482021-02-24 09:20:51 -0800803 }
804 }
805}
806
Jonathan Domandba0c292020-12-02 15:34:13 -0800807/**
808 * Request all the properties for the given D-Bus object and fill out the
809 * related entries in the Redfish OperatingConfig response.
810 *
811 * @param[in,out] aResp Async HTTP response.
812 * @param[in] service D-Bus service name to query.
813 * @param[in] objPath D-Bus object to query.
814 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800815inline void
816 getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
817 const std::string& service,
818 const std::string& objPath)
Jonathan Domandba0c292020-12-02 15:34:13 -0800819{
820 crow::connections::systemBus->async_method_call(
Ed Tanous914e2d52022-01-07 11:38:34 -0800821 [aResp](const boost::system::error_code ec,
Jonathan Domandba0c292020-12-02 15:34:13 -0800822 const OperatingConfigProperties& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700823 if (ec)
824 {
825 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
826 messages::internalError(aResp->res);
827 return;
828 }
829
830 nlohmann::json& json = aResp->res.jsonValue;
831 for (const auto& [key, variant] : properties)
832 {
833 if (key == "AvailableCoreCount")
Jonathan Domandba0c292020-12-02 15:34:13 -0800834 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700835 const size_t* cores = std::get_if<size_t>(&variant);
836 if (cores != nullptr)
Jonathan Domandba0c292020-12-02 15:34:13 -0800837 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700838 json["TotalAvailableCoreCount"] = *cores;
Jonathan Domandba0c292020-12-02 15:34:13 -0800839 }
840 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700841 else if (key == "BaseSpeed")
842 {
843 const uint32_t* speed = std::get_if<uint32_t>(&variant);
844 if (speed != nullptr)
845 {
846 json["BaseSpeedMHz"] = *speed;
847 }
848 }
849 else if (key == "MaxJunctionTemperature")
850 {
851 const uint32_t* temp = std::get_if<uint32_t>(&variant);
852 if (temp != nullptr)
853 {
854 json["MaxJunctionTemperatureCelsius"] = *temp;
855 }
856 }
857 else if (key == "MaxSpeed")
858 {
859 const uint32_t* speed = std::get_if<uint32_t>(&variant);
860 if (speed != nullptr)
861 {
862 json["MaxSpeedMHz"] = *speed;
863 }
864 }
865 else if (key == "PowerLimit")
866 {
867 const uint32_t* tdp = std::get_if<uint32_t>(&variant);
868 if (tdp != nullptr)
869 {
870 json["TDPWatts"] = *tdp;
871 }
872 }
873 else if (key == "TurboProfile")
874 {
875 const auto* turboList =
876 std::get_if<TurboProfileProperty>(&variant);
877 if (turboList == nullptr)
878 {
879 continue;
880 }
881
882 nlohmann::json& turboArray = json["TurboProfile"];
883 turboArray = nlohmann::json::array();
884 for (const auto& [turboSpeed, coreCount] : *turboList)
885 {
886 nlohmann::json::object_t turbo;
887 turbo["ActiveCoreCount"] = coreCount;
888 turbo["MaxSpeedMHz"] = turboSpeed;
889 turboArray.push_back(std::move(turbo));
890 }
891 }
892 else if (key == "BaseSpeedPrioritySettings")
893 {
894 const auto* baseSpeedList =
895 std::get_if<BaseSpeedPrioritySettingsProperty>(&variant);
896 if (baseSpeedList == nullptr)
897 {
898 continue;
899 }
900
901 nlohmann::json& baseSpeedArray =
902 json["BaseSpeedPrioritySettings"];
903 baseSpeedArray = nlohmann::json::array();
904 for (const auto& [baseSpeed, coreList] : *baseSpeedList)
905 {
906 nlohmann::json::object_t speed;
907 speed["CoreCount"] = coreList.size();
908 speed["CoreIDs"] = coreList;
909 speed["BaseSpeedMHz"] = baseSpeed;
910 baseSpeedArray.push_back(std::move(speed));
911 }
912 }
913 }
Jonathan Domandba0c292020-12-02 15:34:13 -0800914 },
915 service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
916 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig");
917}
918
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800919/**
920 * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
921 * property. Main task is to translate error messages into Redfish errors.
922 *
923 * @param[in,out] resp HTTP response.
924 * @param[in] setPropVal Value which we attempted to set.
925 * @param[in] ec D-Bus response error code.
926 * @param[in] msg D-Bus response message.
927 */
928inline void
929 handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
930 const std::string& setPropVal,
931 boost::system::error_code ec,
932 const sdbusplus::message::message& msg)
933{
934 if (!ec)
935 {
936 BMCWEB_LOG_DEBUG << "Set Property succeeded";
937 return;
938 }
939
940 BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
941
942 const sd_bus_error* dbusError = msg.get_error();
943 if (dbusError == nullptr)
944 {
945 messages::internalError(resp->res);
946 return;
947 }
948
949 // The asio error code doesn't know about our custom errors, so we have to
950 // parse the error string. Some of these D-Bus -> Redfish translations are a
951 // stretch, but it's good to try to communicate something vaguely useful.
952 if (strcmp(dbusError->name,
953 "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
954 {
955 // Service did not like the object_path we tried to set.
956 messages::propertyValueIncorrect(
957 resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
958 }
959 else if (strcmp(dbusError->name,
960 "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
961 {
962 // Service indicates we can never change the config for this processor.
963 messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
964 }
965 else if (strcmp(dbusError->name,
966 "xyz.openbmc_project.Common.Error.Unavailable") == 0)
967 {
968 // Service indicates the config cannot be changed right now, but maybe
969 // in a different system state.
970 messages::resourceInStandby(resp->res);
971 }
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800972 else
973 {
974 messages::internalError(resp->res);
975 }
976}
977
978/**
979 * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
980 * validation of the input data, and then set the D-Bus property.
981 *
982 * @param[in,out] resp Async HTTP response.
983 * @param[in] processorId Processor's Id.
984 * @param[in] appliedConfigUri New property value to apply.
985 * @param[in] cpuObjectPath Path of CPU object to modify.
986 * @param[in] serviceMap Service map for CPU object.
987 */
988inline void patchAppliedOperatingConfig(
989 const std::shared_ptr<bmcweb::AsyncResp>& resp,
990 const std::string& processorId, const std::string& appliedConfigUri,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600991 const std::string& cpuObjectPath,
992 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800993{
994 // Check that the property even exists by checking for the interface
995 const std::string* controlService = nullptr;
996 for (const auto& [serviceName, interfaceList] : serviceMap)
997 {
998 if (std::find(interfaceList.begin(), interfaceList.end(),
999 "xyz.openbmc_project.Control.Processor."
1000 "CurrentOperatingConfig") != interfaceList.end())
1001 {
1002 controlService = &serviceName;
1003 break;
1004 }
1005 }
1006
1007 if (controlService == nullptr)
1008 {
1009 messages::internalError(resp->res);
1010 return;
1011 }
1012
1013 // Check that the config URI is a child of the cpu URI being patched.
1014 std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
1015 expectedPrefix += processorId;
1016 expectedPrefix += "/OperatingConfigs/";
1017 if (!boost::starts_with(appliedConfigUri, expectedPrefix) ||
1018 expectedPrefix.size() == appliedConfigUri.size())
1019 {
1020 messages::propertyValueIncorrect(
1021 resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
1022 return;
1023 }
1024
1025 // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
1026 // direct child of the CPU object.
1027 // Strip the expectedPrefix from the config URI to get the "filename", and
1028 // append to the CPU's path.
1029 std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
1030 sdbusplus::message::object_path configPath(cpuObjectPath);
1031 configPath /= configBaseName;
1032
1033 BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
1034
1035 // Set the property, with handler to check error responses
1036 crow::connections::systemBus->async_method_call(
Ed Tanous914e2d52022-01-07 11:38:34 -08001037 [resp, appliedConfigUri](const boost::system::error_code ec,
1038 const sdbusplus::message::message& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001039 handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001040 },
1041 *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
1042 "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Ed Tanous168e20c2021-12-13 14:39:53 -08001043 "AppliedConfig", dbus::utility::DbusVariantType(std::move(configPath)));
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001044}
1045
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001046inline void requestRoutesOperatingConfigCollection(App& app)
Jonathan Domandba0c292020-12-02 15:34:13 -08001047{
Jonathan Domandba0c292020-12-02 15:34:13 -08001048
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001049 BMCWEB_ROUTE(
1050 app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
Ed Tanoused398212021-06-09 17:05:54 -07001051 .privileges(redfish::privileges::getOperatingConfigCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001052 .methods(boost::beast::http::verb::get)(
1053 [&app](const crow::Request& req,
1054 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1055 const std::string& cpuName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001056 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001057 {
1058 return;
1059 }
1060 asyncResp->res.jsonValue["@odata.type"] =
1061 "#OperatingConfigCollection.OperatingConfigCollection";
1062 asyncResp->res.jsonValue["@odata.id"] = req.url;
1063 asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
1064
1065 // First find the matching CPU object so we know how to
1066 // constrain our search for related Config objects.
1067 crow::connections::systemBus->async_method_call(
1068 [asyncResp, cpuName](
1069 const boost::system::error_code ec,
1070 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
1071 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001072 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001073 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1074 << ec.message();
1075 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001076 return;
1077 }
Jonathan Domandba0c292020-12-02 15:34:13 -08001078
Ed Tanous002d39b2022-05-31 08:59:27 -07001079 for (const std::string& object : objects)
1080 {
1081 if (!boost::ends_with(object, cpuName))
1082 {
1083 continue;
1084 }
George Liu0fda0f12021-11-16 10:06:17 +08001085
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 // Not expected that there will be multiple matching
1087 // CPU objects, but if there are just use the first
1088 // one.
Jonathan Domandba0c292020-12-02 15:34:13 -08001089
Ed Tanous002d39b2022-05-31 08:59:27 -07001090 // Use the common search routine to construct the
1091 // Collection of all Config objects under this CPU.
1092 collection_util::getCollectionMembers(
1093 asyncResp,
1094 "/redfish/v1/Systems/system/Processors/" + cpuName +
1095 "/OperatingConfigs",
1096 {"xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"},
1097 object.c_str());
1098 return;
1099 }
1100 },
1101 "xyz.openbmc_project.ObjectMapper",
1102 "/xyz/openbmc_project/object_mapper",
1103 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
1104 "/xyz/openbmc_project/inventory", 0,
1105 std::array<const char*, 1>{
1106 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"});
George Liu0fda0f12021-11-16 10:06:17 +08001107 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001108}
1109
1110inline void requestRoutesOperatingConfig(App& app)
1111{
1112 BMCWEB_ROUTE(
1113 app,
1114 "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001115 .privileges(redfish::privileges::getOperatingConfig)
Ed Tanous002d39b2022-05-31 08:59:27 -07001116 .methods(boost::beast::http::verb::get)(
1117 [&app](const crow::Request& req,
1118 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1119 const std::string& cpuName, const std::string& configName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001120 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 {
1122 return;
1123 }
1124 // Ask for all objects implementing OperatingConfig so we can search
1125 // for one with a matching name
1126 crow::connections::systemBus->async_method_call(
1127 [asyncResp, cpuName, configName, reqUrl{req.url}](
1128 boost::system::error_code ec,
1129 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1130 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001131 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001132 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1133 << ec.message();
1134 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001135 return;
1136 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 const std::string expectedEnding = cpuName + '/' + configName;
1138 for (const auto& [objectPath, serviceMap] : subtree)
1139 {
1140 // Ignore any configs without matching cpuX/configY
1141 if (!boost::ends_with(objectPath, expectedEnding) ||
1142 serviceMap.empty())
1143 {
1144 continue;
1145 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001146
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 nlohmann::json& json = asyncResp->res.jsonValue;
1148 json["@odata.type"] = "#OperatingConfig.v1_0_0.OperatingConfig";
1149 json["@odata.id"] = reqUrl;
1150 json["Name"] = "Processor Profile";
1151 json["Id"] = configName;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001152
Ed Tanous002d39b2022-05-31 08:59:27 -07001153 // Just use the first implementation of the object - not
1154 // expected that there would be multiple matching
1155 // services
1156 getOperatingConfigData(asyncResp, serviceMap.begin()->first,
1157 objectPath);
1158 return;
1159 }
1160 messages::resourceNotFound(asyncResp->res, "OperatingConfig",
1161 configName);
1162 },
1163 "xyz.openbmc_project.ObjectMapper",
1164 "/xyz/openbmc_project/object_mapper",
1165 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1166 "/xyz/openbmc_project/inventory", 0,
1167 std::array<const char*, 1>{
1168 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001169 });
1170}
Jonathan Domandba0c292020-12-02 15:34:13 -08001171
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001172inline void requestRoutesProcessorCollection(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001173{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001174 /**
1175 * Functions triggers appropriate requests on DBus
1176 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001177 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/")
Ed Tanoused398212021-06-09 17:05:54 -07001178 .privileges(redfish::privileges::getProcessorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001179 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001180 [&app](const crow::Request& req,
1181 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001182 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001183 {
1184 return;
1185 }
1186 asyncResp->res.jsonValue["@odata.type"] =
1187 "#ProcessorCollection.ProcessorCollection";
1188 asyncResp->res.jsonValue["Name"] = "Processor Collection";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001189
Ed Tanous002d39b2022-05-31 08:59:27 -07001190 asyncResp->res.jsonValue["@odata.id"] =
1191 "/redfish/v1/Systems/system/Processors";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001192
Ed Tanous002d39b2022-05-31 08:59:27 -07001193 collection_util::getCollectionMembers(
1194 asyncResp, "/redfish/v1/Systems/system/Processors",
1195 std::vector<const char*>(processorInterfaces.begin(),
1196 processorInterfaces.end()));
1197 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001198}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001199
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001200inline void requestRoutesProcessor(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001201{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001202 /**
1203 * Functions triggers appropriate requests on DBus
1204 */
Gunnar Millsac6a4442020-10-14 14:55:29 -05001205
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001206 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001207 .privileges(redfish::privileges::getProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001208 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001209 [&app](const crow::Request& req,
1210 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1211 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001212 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001213 {
1214 return;
1215 }
1216 asyncResp->res.jsonValue["@odata.type"] =
1217 "#Processor.v1_11_0.Processor";
1218 asyncResp->res.jsonValue["@odata.id"] =
1219 "/redfish/v1/Systems/system/Processors/" + processorId;
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001220
Ed Tanous8a592812022-06-04 09:06:59 -07001221 getProcessorObject(
1222 asyncResp, processorId,
1223 std::bind_front(getProcessorData, asyncResp, processorId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001224 });
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001225
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001226 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001227 .privileges(redfish::privileges::patchProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001228 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001229 [&app](const crow::Request& req,
1230 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1231 const std::string& processorId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001232 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001233 {
1234 return;
1235 }
1236 std::optional<nlohmann::json> appliedConfigJson;
1237 if (!json_util::readJsonPatch(req, asyncResp->res,
1238 "AppliedOperatingConfig",
1239 appliedConfigJson))
1240 {
1241 return;
1242 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001243
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 std::string appliedConfigUri;
1245 if (appliedConfigJson)
1246 {
1247 if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
1248 "@odata.id", appliedConfigUri))
1249 {
1250 return;
1251 }
1252 // Check for 404 and find matching D-Bus object, then run
1253 // property patch handlers if that all succeeds.
Ed Tanous8a592812022-06-04 09:06:59 -07001254 getProcessorObject(asyncResp, processorId,
1255 std::bind_front(patchAppliedOperatingConfig,
1256 asyncResp, processorId,
1257 appliedConfigUri));
Ed Tanous002d39b2022-05-31 08:59:27 -07001258 }
1259 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001260}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001261
1262} // namespace redfish