blob: f304eb86c03633f03fcb2d0a48d3632bb4696c8c [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 Tanoused398212021-06-09 17:05:54 -070025#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070026#include <sdbusplus/asio/property.hpp>
Jonathan Domandba0c292020-12-02 15:34:13 -080027#include <sdbusplus/message/native_types.hpp>
28#include <sdbusplus/utility/dedup_variant.hpp>
Gunnar Millsac6a4442020-10-14 14:55:29 -050029#include <utils/collection.hpp>
30#include <utils/json_utils.hpp>
31
32namespace redfish
33{
34
Jonathan Domanc9514482021-02-24 09:20:51 -080035// Interfaces which imply a D-Bus object represents a Processor
36constexpr std::array<const char*, 2> processorInterfaces = {
37 "xyz.openbmc_project.Inventory.Item.Cpu",
38 "xyz.openbmc_project.Inventory.Item.Accelerator"};
Jonathan Doman2bab9832020-12-02 15:27:40 -080039
Sharad Yadav71b82f22021-05-10 15:11:39 +053040/**
41 * @brief Fill out uuid info of a processor by
42 * requesting data from the given D-Bus object.
43 *
44 * @param[in,out] aResp Async HTTP response.
45 * @param[in] service D-Bus service to query.
46 * @param[in] objPath D-Bus object to query.
47 */
48inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
49 const std::string& service,
50 const std::string& objPath)
51{
52 BMCWEB_LOG_DEBUG << "Get Processor UUID";
Jonathan Doman1e1e5982021-06-11 09:36:17 -070053 sdbusplus::asio::getProperty<std::string>(
54 *crow::connections::systemBus, service, objPath,
55 "xyz.openbmc_project.Common.UUID", "UUID",
56 [objPath, aResp{std::move(aResp)}](const boost::system::error_code ec,
57 const std::string& property) {
Sharad Yadav71b82f22021-05-10 15:11:39 +053058 if (ec)
59 {
60 BMCWEB_LOG_DEBUG << "DBUS response error";
61 messages::internalError(aResp->res);
62 return;
63 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070064 aResp->res.jsonValue["UUID"] = property;
65 });
Sharad Yadav71b82f22021-05-10 15:11:39 +053066}
67
Ed Tanous711ac7a2021-12-20 09:34:41 -080068inline void getCpuDataByInterface(
69 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
70 const dbus::utility::DBusInteracesMap& cpuInterfacesProperties)
Gunnar Millsac6a4442020-10-14 14:55:29 -050071{
72 BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
73
Chicago Duana1649ec2021-03-30 16:54:58 +080074 // Set the default value of state
75 aResp->res.jsonValue["Status"]["State"] = "Enabled";
76 aResp->res.jsonValue["Status"]["Health"] = "OK";
Gunnar Millsac6a4442020-10-14 14:55:29 -050077
78 for (const auto& interface : cpuInterfacesProperties)
79 {
80 for (const auto& property : interface.second)
81 {
Chicago Duana1649ec2021-03-30 16:54:58 +080082 if (property.first == "Present")
Gunnar Millsac6a4442020-10-14 14:55:29 -050083 {
Chicago Duana1649ec2021-03-30 16:54:58 +080084 const bool* cpuPresent = std::get_if<bool>(&property.second);
85 if (cpuPresent == nullptr)
Gunnar Millsac6a4442020-10-14 14:55:29 -050086 {
87 // Important property not in desired type
88 messages::internalError(aResp->res);
89 return;
90 }
Ed Tanouse05aec52022-01-25 10:28:56 -080091 if (!*cpuPresent)
Gunnar Millsac6a4442020-10-14 14:55:29 -050092 {
Chicago Duana1649ec2021-03-30 16:54:58 +080093 // Slot is not populated
Gunnar Millsac6a4442020-10-14 14:55:29 -050094 aResp->res.jsonValue["Status"]["State"] = "Absent";
Chicago Duana1649ec2021-03-30 16:54:58 +080095 }
96 }
97 else if (property.first == "Functional")
98 {
99 const bool* cpuFunctional = std::get_if<bool>(&property.second);
100 if (cpuFunctional == nullptr)
101 {
102 messages::internalError(aResp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500103 return;
104 }
Ed Tanouse05aec52022-01-25 10:28:56 -0800105 if (!*cpuFunctional)
Chicago Duana1649ec2021-03-30 16:54:58 +0800106 {
107 aResp->res.jsonValue["Status"]["Health"] = "Critical";
108 }
109 }
110 else if (property.first == "CoreCount")
111 {
112 const uint16_t* coresCount =
113 std::get_if<uint16_t>(&property.second);
114 if (coresCount == nullptr)
115 {
116 messages::internalError(aResp->res);
117 return;
118 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500119 aResp->res.jsonValue["TotalCores"] = *coresCount;
120 }
Jonathan Domandc3fa662020-10-26 23:10:24 -0700121 else if (property.first == "MaxSpeedInMhz")
122 {
123 const uint32_t* value = std::get_if<uint32_t>(&property.second);
124 if (value != nullptr)
125 {
126 aResp->res.jsonValue["MaxSpeedMHz"] = *value;
127 }
128 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500129 else if (property.first == "Socket")
130 {
131 const std::string* value =
132 std::get_if<std::string>(&property.second);
133 if (value != nullptr)
134 {
135 aResp->res.jsonValue["Socket"] = *value;
136 }
137 }
138 else if (property.first == "ThreadCount")
139 {
Jonathan Domandc3fa662020-10-26 23:10:24 -0700140 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500141 if (value != nullptr)
142 {
143 aResp->res.jsonValue["TotalThreads"] = *value;
144 }
145 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700146 else if (property.first == "EffectiveFamily")
Gunnar Millsac6a4442020-10-14 14:55:29 -0500147 {
Brandon Kim1930fbd2021-09-14 17:52:51 -0700148 const uint16_t* value = std::get_if<uint16_t>(&property.second);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500149 if (value != nullptr)
150 {
151 aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800152 "0x" + intToHexString(*value, 4);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500153 }
154 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700155 else if (property.first == "EffectiveModel")
156 {
157 const uint16_t* value = std::get_if<uint16_t>(&property.second);
158 if (value == nullptr)
159 {
160 messages::internalError(aResp->res);
161 return;
162 }
163 aResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800164 "0x" + intToHexString(*value, 4);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700165 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500166 else if (property.first == "Id")
167 {
168 const uint64_t* value = std::get_if<uint64_t>(&property.second);
169 if (value != nullptr && *value != 0)
170 {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500171 aResp->res
172 .jsonValue["ProcessorId"]["IdentificationRegisters"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800173 "0x" + intToHexString(*value, 16);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500174 }
175 }
Brandon Kim1930fbd2021-09-14 17:52:51 -0700176 else if (property.first == "Microcode")
177 {
178 const uint32_t* value = std::get_if<uint32_t>(&property.second);
179 if (value == nullptr)
180 {
181 messages::internalError(aResp->res);
182 return;
183 }
184 aResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800185 "0x" + intToHexString(*value, 8);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700186 }
187 else if (property.first == "Step")
188 {
189 const uint16_t* value = std::get_if<uint16_t>(&property.second);
190 if (value == nullptr)
191 {
192 messages::internalError(aResp->res);
193 return;
194 }
195 aResp->res.jsonValue["ProcessorId"]["Step"] =
Ed Tanous866e4862022-02-17 11:40:25 -0800196 "0x" + intToHexString(*value, 4);
Brandon Kim1930fbd2021-09-14 17:52:51 -0700197 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500198 }
199 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500200}
201
zhanghch058d1b46d2021-04-01 11:18:24 +0800202inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500203 const std::string& cpuId,
204 const std::string& service,
205 const std::string& objPath)
206{
207 BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
208
209 crow::connections::systemBus->async_method_call(
210 [cpuId, service, objPath, aResp{std::move(aResp)}](
211 const boost::system::error_code ec,
212 const dbus::utility::ManagedObjectType& dbusData) {
213 if (ec)
214 {
215 BMCWEB_LOG_DEBUG << "DBUS response error";
216 messages::internalError(aResp->res);
217 return;
218 }
219 aResp->res.jsonValue["Id"] = cpuId;
220 aResp->res.jsonValue["Name"] = "Processor";
221 aResp->res.jsonValue["ProcessorType"] = "CPU";
222
223 bool slotPresent = false;
224 std::string corePath = objPath + "/core";
225 size_t totalCores = 0;
226 for (const auto& object : dbusData)
227 {
228 if (object.first.str == objPath)
229 {
230 getCpuDataByInterface(aResp, object.second);
231 }
232 else if (boost::starts_with(object.first.str, corePath))
233 {
234 for (const auto& interface : object.second)
235 {
236 if (interface.first ==
237 "xyz.openbmc_project.Inventory.Item")
238 {
239 for (const auto& property : interface.second)
240 {
241 if (property.first == "Present")
242 {
243 const bool* present =
244 std::get_if<bool>(&property.second);
245 if (present != nullptr)
246 {
Ed Tanouse05aec52022-01-25 10:28:56 -0800247 if (*present)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500248 {
249 slotPresent = true;
250 totalCores++;
251 }
252 }
253 }
254 }
255 }
256 }
257 }
258 }
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)
265 {
266 // Slot is not populated, set status end return
267 aResp->res.jsonValue["Status"]["State"] = "Absent";
268 aResp->res.jsonValue["Status"]["Health"] = "OK";
269 }
270 aResp->res.jsonValue["TotalCores"] = totalCores;
271 }
272 return;
273 },
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) {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500288 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")
298 {
299 const std::string* sn =
300 std::get_if<std::string>(&property.second);
301 if (sn != nullptr && !sn->empty())
302 {
303 aResp->res.jsonValue["SerialNumber"] = *sn;
304 }
305 }
306 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"] =
328 "x86";
329 aResp->res.jsonValue["InstructionSet"] = "x86-64";
330 }
331 else if (mfg->find("IBM") != std::string::npos)
332 {
333 aResp->res.jsonValue["ProcessorArchitecture"] =
334 "Power";
335 aResp->res.jsonValue["InstructionSet"] = "PowerISA";
336 }
337 }
338 }
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600339 else if (property.first == "PartNumber")
340 {
341 const std::string* partNumber =
342 std::get_if<std::string>(&property.second);
343
344 if (partNumber == nullptr)
345 {
346 messages::internalError(aResp->res);
347 return;
348 }
349 aResp->res.jsonValue["PartNumber"] = *partNumber;
350 }
351 else if (property.first == "SparePartNumber")
352 {
353 const std::string* sparePartNumber =
354 std::get_if<std::string>(&property.second);
355
356 if (sparePartNumber == nullptr)
357 {
358 messages::internalError(aResp->res);
359 return;
360 }
361 aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
362 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500363 }
364 },
365 service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
366 "xyz.openbmc_project.Inventory.Decorator.Asset");
367}
368
zhanghch058d1b46d2021-04-01 11:18:24 +0800369inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
Gunnar Millsac6a4442020-10-14 14:55:29 -0500370 const std::string& service,
371 const std::string& objPath)
372{
373 BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
374 crow::connections::systemBus->async_method_call(
375 [objPath, aResp{std::move(aResp)}](
376 const boost::system::error_code ec,
377 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800378 std::string, dbus::utility::DbusVariantType>& properties) {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500379 if (ec)
380 {
381 BMCWEB_LOG_DEBUG << "DBUS response error";
382 messages::internalError(aResp->res);
383 return;
384 }
385
386 for (const auto& property : properties)
387 {
388 if (property.first == "Version")
389 {
390 const std::string* ver =
391 std::get_if<std::string>(&property.second);
392 if (ver != nullptr)
393 {
394 aResp->res.jsonValue["Version"] = *ver;
395 }
396 break;
397 }
398 }
399 },
400 service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
401 "xyz.openbmc_project.Inventory.Decorator.Revision");
402}
403
zhanghch058d1b46d2021-04-01 11:18:24 +0800404inline void getAcceleratorDataByService(
405 std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
406 const std::string& service, const std::string& objPath)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500407{
408 BMCWEB_LOG_DEBUG
409 << "Get available system Accelerator resources by service.";
410 crow::connections::systemBus->async_method_call(
411 [acclrtrId, aResp{std::move(aResp)}](
412 const boost::system::error_code ec,
413 const boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800414 std::string, dbus::utility::DbusVariantType>& properties) {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500415 if (ec)
416 {
417 BMCWEB_LOG_DEBUG << "DBUS response error";
418 messages::internalError(aResp->res);
419 return;
420 }
421 aResp->res.jsonValue["Id"] = acclrtrId;
422 aResp->res.jsonValue["Name"] = "Processor";
423 const bool* accPresent = nullptr;
424 const bool* accFunctional = nullptr;
425
426 for (const auto& property : properties)
427 {
428 if (property.first == "Functional")
429 {
430 accFunctional = std::get_if<bool>(&property.second);
431 }
432 else if (property.first == "Present")
433 {
434 accPresent = std::get_if<bool>(&property.second);
435 }
436 }
437
438 std::string state = "Enabled";
439 std::string health = "OK";
440
Ed Tanouse05aec52022-01-25 10:28:56 -0800441 if (accPresent != nullptr && !*accPresent)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500442 {
443 state = "Absent";
444 }
445
Ed Tanouse05aec52022-01-25 10:28:56 -0800446 if ((accFunctional != nullptr) && !*accFunctional)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500447 {
448 if (state == "Enabled")
449 {
450 health = "Critical";
451 }
452 }
453
454 aResp->res.jsonValue["Status"]["State"] = state;
455 aResp->res.jsonValue["Status"]["Health"] = health;
456 aResp->res.jsonValue["ProcessorType"] = "Accelerator";
457 },
458 service, objPath, "org.freedesktop.DBus.Properties", "GetAll", "");
459}
460
Jonathan Domandba0c292020-12-02 15:34:13 -0800461// OperatingConfig D-Bus Types
462using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
463using BaseSpeedPrioritySettingsProperty =
464 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
465// uint32_t and size_t may or may not be the same type, requiring a dedup'd
466// variant
Ed Tanous168e20c2021-12-13 14:39:53 -0800467using OperatingConfigProperties =
468 std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>;
Jonathan Domandba0c292020-12-02 15:34:13 -0800469
470/**
471 * Fill out the HighSpeedCoreIDs in a Processor resource from the given
472 * OperatingConfig D-Bus property.
473 *
474 * @param[in,out] aResp Async HTTP response.
475 * @param[in] baseSpeedSettings Full list of base speed priority groups,
476 * to use to determine the list of high
477 * speed cores.
478 */
479inline void highSpeedCoreIdsHandler(
zhanghch058d1b46d2021-04-01 11:18:24 +0800480 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800481 const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
482{
483 // The D-Bus property does not indicate which bucket is the "high
484 // priority" group, so let's discern that by looking for the one with
485 // highest base frequency.
486 auto highPriorityGroup = baseSpeedSettings.cend();
487 uint32_t highestBaseSpeed = 0;
488 for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
489 ++it)
490 {
491 const uint32_t baseFreq = std::get<uint32_t>(*it);
492 if (baseFreq > highestBaseSpeed)
493 {
494 highestBaseSpeed = baseFreq;
495 highPriorityGroup = it;
496 }
497 }
498
499 nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
500 jsonCoreIds = nlohmann::json::array();
501
502 // There may not be any entries in the D-Bus property, so only populate
503 // if there was actually something there.
504 if (highPriorityGroup != baseSpeedSettings.cend())
505 {
506 jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
507 }
508}
509
510/**
511 * Fill out OperatingConfig related items in a Processor resource by requesting
512 * data from the given D-Bus object.
513 *
514 * @param[in,out] aResp Async HTTP response.
515 * @param[in] cpuId CPU D-Bus name.
516 * @param[in] service D-Bus service to query.
517 * @param[in] objPath D-Bus object to query.
518 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800519inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jonathan Domandba0c292020-12-02 15:34:13 -0800520 const std::string& cpuId,
521 const std::string& service,
522 const std::string& objPath)
523{
524 BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
525
526 // First, GetAll CurrentOperatingConfig properties on the object
527 crow::connections::systemBus->async_method_call(
528 [aResp, cpuId, service](
529 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -0800530 const std::vector<std::pair<
531 std::string, dbus::utility::DbusVariantType>>& properties) {
Jonathan Domandba0c292020-12-02 15:34:13 -0800532 if (ec)
533 {
534 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
535 << ec.message();
536 messages::internalError(aResp->res);
537 return;
538 }
539
540 nlohmann::json& json = aResp->res.jsonValue;
541
542 for (const auto& [dbusPropName, variantVal] : properties)
543 {
544 if (dbusPropName == "AppliedConfig")
545 {
546 const sdbusplus::message::object_path* dbusPathWrapper =
547 std::get_if<sdbusplus::message::object_path>(
548 &variantVal);
549 if (dbusPathWrapper == nullptr)
550 {
551 continue;
552 }
553
554 const std::string& dbusPath = dbusPathWrapper->str;
555 std::string uri = "/redfish/v1/Systems/system/Processors/" +
556 cpuId + "/OperatingConfigs";
557 json["OperatingConfigs"] = {{"@odata.id", uri}};
558
559 // Reuse the D-Bus config object name for the Redfish
560 // URI
561 size_t baseNamePos = dbusPath.rfind('/');
562 if (baseNamePos == std::string::npos ||
563 baseNamePos == (dbusPath.size() - 1))
564 {
565 // If the AppliedConfig was somehow not a valid path,
566 // skip adding any more properties, since everything
567 // else is tied to this applied config.
568 messages::internalError(aResp->res);
569 break;
570 }
571 uri += '/';
572 uri += dbusPath.substr(baseNamePos + 1);
573 json["AppliedOperatingConfig"] = {{"@odata.id", uri}};
574
575 // Once we found the current applied config, queue another
576 // request to read the base freq core ids out of that
577 // config.
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700578 sdbusplus::asio::getProperty<
579 BaseSpeedPrioritySettingsProperty>(
580 *crow::connections::systemBus, service, dbusPath,
581 "xyz.openbmc_project.Inventory.Item.Cpu."
582 "OperatingConfig",
583 "BaseSpeedPrioritySettings",
584 [aResp](const boost::system::error_code ec,
585 const BaseSpeedPrioritySettingsProperty&
586 baseSpeedList) {
Jonathan Domandba0c292020-12-02 15:34:13 -0800587 if (ec)
588 {
589 BMCWEB_LOG_WARNING
590 << "D-Bus Property Get error: " << ec;
591 messages::internalError(aResp->res);
592 return;
593 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700594
595 highSpeedCoreIdsHandler(aResp, baseSpeedList);
596 });
Jonathan Domandba0c292020-12-02 15:34:13 -0800597 }
598 else if (dbusPropName == "BaseSpeedPriorityEnabled")
599 {
600 const bool* state = std::get_if<bool>(&variantVal);
601 if (state != nullptr)
602 {
603 json["BaseSpeedPriorityState"] =
604 *state ? "Enabled" : "Disabled";
605 }
606 }
607 }
608 },
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) {
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600631 if (ec)
632 {
633 BMCWEB_LOG_DEBUG << "DBUS response error";
634 messages::internalError(aResp->res);
635 return;
636 }
637
SunnySrivastava1984cba4f442021-01-07 12:48:16 -0600638 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700639 property;
640 });
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) {
661 if (ec)
Jonathan Doman49e429c2021-03-03 13:11:44 -0800662 {
663 BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
664 messages::internalError(aResp->res);
665 return;
666 }
667 aResp->res
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700668 .jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] = id;
669 });
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 {
Gunnar Millsac6a4442020-10-14 14:55:29 -0500694 if (ec)
695 {
Jonathan Domanc9514482021-02-24 09:20:51 -0800696 BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
697 messages::internalError(resp->res);
Gunnar Millsac6a4442020-10-14 14:55:29 -0500698 return;
699 }
Jonathan Doman2bab9832020-12-02 15:27:40 -0800700 for (const auto& [objectPath, serviceMap] : subtree)
Gunnar Millsac6a4442020-10-14 14:55:29 -0500701 {
Jonathan Doman2bab9832020-12-02 15:27:40 -0800702 // 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 {
Jonathan Doman2bab9832020-12-02 15:27:40 -0800705 continue;
706 }
707
Jonathan Domanc9514482021-02-24 09:20:51 -0800708 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)
Jonathan Doman2bab9832020-12-02 15:27:40 -0800712 for (const auto& [serviceName, interfaceList] : serviceMap)
713 {
Jonathan Domanc9514482021-02-24 09:20:51 -0800714 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 {
Jonathan Domanc9514482021-02-24 09:20:51 -0800719 found = true;
720 break;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500721 }
Gunnar Millsac6a4442020-10-14 14:55:29 -0500722 }
Jonathan Domanc9514482021-02-24 09:20:51 -0800723
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
734 handler(resp, processorId, objectPath, serviceMap);
Jonathan Doman2bab9832020-12-02 15:27:40 -0800735 return;
Gunnar Millsac6a4442020-10-14 14:55:29 -0500736 }
Jonathan Domanc9514482021-02-24 09:20:51 -0800737 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) {
823 if (ec)
824 {
825 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
826 << ec.message();
827 messages::internalError(aResp->res);
828 return;
829 }
830
831 nlohmann::json& json = aResp->res.jsonValue;
832 for (const auto& [key, variant] : properties)
833 {
834 if (key == "AvailableCoreCount")
835 {
836 const size_t* cores = std::get_if<size_t>(&variant);
837 if (cores != nullptr)
838 {
839 json["TotalAvailableCoreCount"] = *cores;
840 }
841 }
842 else if (key == "BaseSpeed")
843 {
844 const uint32_t* speed = std::get_if<uint32_t>(&variant);
845 if (speed != nullptr)
846 {
847 json["BaseSpeedMHz"] = *speed;
848 }
849 }
850 else if (key == "MaxJunctionTemperature")
851 {
852 const uint32_t* temp = std::get_if<uint32_t>(&variant);
853 if (temp != nullptr)
854 {
855 json["MaxJunctionTemperatureCelsius"] = *temp;
856 }
857 }
858 else if (key == "MaxSpeed")
859 {
860 const uint32_t* speed = std::get_if<uint32_t>(&variant);
861 if (speed != nullptr)
862 {
863 json["MaxSpeedMHz"] = *speed;
864 }
865 }
866 else if (key == "PowerLimit")
867 {
868 const uint32_t* tdp = std::get_if<uint32_t>(&variant);
869 if (tdp != nullptr)
870 {
871 json["TDPWatts"] = *tdp;
872 }
873 }
874 else if (key == "TurboProfile")
875 {
876 const auto* turboList =
877 std::get_if<TurboProfileProperty>(&variant);
878 if (turboList == nullptr)
879 {
880 continue;
881 }
882
883 nlohmann::json& turboArray = json["TurboProfile"];
884 turboArray = nlohmann::json::array();
885 for (const auto& [turboSpeed, coreCount] : *turboList)
886 {
887 turboArray.push_back({{"ActiveCoreCount", coreCount},
888 {"MaxSpeedMHz", turboSpeed}});
889 }
890 }
891 else if (key == "BaseSpeedPrioritySettings")
892 {
893 const auto* baseSpeedList =
894 std::get_if<BaseSpeedPrioritySettingsProperty>(
895 &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 baseSpeedArray.push_back(
907 {{"CoreCount", coreList.size()},
908 {"CoreIDs", coreList},
909 {"BaseSpeedMHz", baseSpeed}});
910 }
911 }
912 }
913 },
914 service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
915 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig");
916}
917
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800918/**
919 * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
920 * property. Main task is to translate error messages into Redfish errors.
921 *
922 * @param[in,out] resp HTTP response.
923 * @param[in] setPropVal Value which we attempted to set.
924 * @param[in] ec D-Bus response error code.
925 * @param[in] msg D-Bus response message.
926 */
927inline void
928 handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
929 const std::string& setPropVal,
930 boost::system::error_code ec,
931 const sdbusplus::message::message& msg)
932{
933 if (!ec)
934 {
935 BMCWEB_LOG_DEBUG << "Set Property succeeded";
936 return;
937 }
938
939 BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
940
941 const sd_bus_error* dbusError = msg.get_error();
942 if (dbusError == nullptr)
943 {
944 messages::internalError(resp->res);
945 return;
946 }
947
948 // The asio error code doesn't know about our custom errors, so we have to
949 // parse the error string. Some of these D-Bus -> Redfish translations are a
950 // stretch, but it's good to try to communicate something vaguely useful.
951 if (strcmp(dbusError->name,
952 "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
953 {
954 // Service did not like the object_path we tried to set.
955 messages::propertyValueIncorrect(
956 resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
957 }
958 else if (strcmp(dbusError->name,
959 "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
960 {
961 // Service indicates we can never change the config for this processor.
962 messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
963 }
964 else if (strcmp(dbusError->name,
965 "xyz.openbmc_project.Common.Error.Unavailable") == 0)
966 {
967 // Service indicates the config cannot be changed right now, but maybe
968 // in a different system state.
969 messages::resourceInStandby(resp->res);
970 }
971 else if (strcmp(dbusError->name,
972 "xyz.openbmc_project.Common.Device.Error.WriteFailure") ==
973 0)
974 {
975 // Service tried to change the config, but it failed.
976 messages::operationFailed(resp->res);
977 }
978 else
979 {
980 messages::internalError(resp->res);
981 }
982}
983
984/**
985 * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
986 * validation of the input data, and then set the D-Bus property.
987 *
988 * @param[in,out] resp Async HTTP response.
989 * @param[in] processorId Processor's Id.
990 * @param[in] appliedConfigUri New property value to apply.
991 * @param[in] cpuObjectPath Path of CPU object to modify.
992 * @param[in] serviceMap Service map for CPU object.
993 */
994inline void patchAppliedOperatingConfig(
995 const std::shared_ptr<bmcweb::AsyncResp>& resp,
996 const std::string& processorId, const std::string& appliedConfigUri,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -0600997 const std::string& cpuObjectPath,
998 const dbus::utility::MapperServiceMap& serviceMap)
Jonathan Doman3cde86f2020-12-02 14:50:45 -0800999{
1000 // Check that the property even exists by checking for the interface
1001 const std::string* controlService = nullptr;
1002 for (const auto& [serviceName, interfaceList] : serviceMap)
1003 {
1004 if (std::find(interfaceList.begin(), interfaceList.end(),
1005 "xyz.openbmc_project.Control.Processor."
1006 "CurrentOperatingConfig") != interfaceList.end())
1007 {
1008 controlService = &serviceName;
1009 break;
1010 }
1011 }
1012
1013 if (controlService == nullptr)
1014 {
1015 messages::internalError(resp->res);
1016 return;
1017 }
1018
1019 // Check that the config URI is a child of the cpu URI being patched.
1020 std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
1021 expectedPrefix += processorId;
1022 expectedPrefix += "/OperatingConfigs/";
1023 if (!boost::starts_with(appliedConfigUri, expectedPrefix) ||
1024 expectedPrefix.size() == appliedConfigUri.size())
1025 {
1026 messages::propertyValueIncorrect(
1027 resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
1028 return;
1029 }
1030
1031 // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
1032 // direct child of the CPU object.
1033 // Strip the expectedPrefix from the config URI to get the "filename", and
1034 // append to the CPU's path.
1035 std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
1036 sdbusplus::message::object_path configPath(cpuObjectPath);
1037 configPath /= configBaseName;
1038
1039 BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
1040
1041 // Set the property, with handler to check error responses
1042 crow::connections::systemBus->async_method_call(
Ed Tanous914e2d52022-01-07 11:38:34 -08001043 [resp, appliedConfigUri](const boost::system::error_code ec,
1044 const sdbusplus::message::message& msg) {
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001045 handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
1046 },
1047 *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
1048 "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
Ed Tanous168e20c2021-12-13 14:39:53 -08001049 "AppliedConfig", dbus::utility::DbusVariantType(std::move(configPath)));
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001050}
1051
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001052inline void requestRoutesOperatingConfigCollection(App& app)
Jonathan Domandba0c292020-12-02 15:34:13 -08001053{
Jonathan Domandba0c292020-12-02 15:34:13 -08001054
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001055 BMCWEB_ROUTE(
1056 app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
Ed Tanoused398212021-06-09 17:05:54 -07001057 .privileges(redfish::privileges::getOperatingConfigCollection)
George Liu0fda0f12021-11-16 10:06:17 +08001058 .methods(
1059 boost::beast::http::verb::get)([](const crow::Request& req,
1060 const std::shared_ptr<
1061 bmcweb::AsyncResp>& asyncResp,
1062 const std::string& cpuName) {
1063 asyncResp->res.jsonValue["@odata.type"] =
1064 "#OperatingConfigCollection.OperatingConfigCollection";
1065 asyncResp->res.jsonValue["@odata.id"] = req.url;
1066 asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
Jonathan Domandba0c292020-12-02 15:34:13 -08001067
George Liu0fda0f12021-11-16 10:06:17 +08001068 // First find the matching CPU object so we know how to
1069 // constrain our search for related Config objects.
1070 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001071 [asyncResp,
1072 cpuName](const boost::system::error_code ec,
1073 const dbus::utility::MapperGetSubTreePathsResponse&
1074 objects) {
George Liu0fda0f12021-11-16 10:06:17 +08001075 if (ec)
1076 {
1077 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1078 << ec.message();
1079 messages::internalError(asyncResp->res);
1080 return;
1081 }
1082
1083 for (const std::string& object : objects)
1084 {
1085 if (!boost::ends_with(object, cpuName))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001086 {
George Liu0fda0f12021-11-16 10:06:17 +08001087 continue;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001088 }
Jonathan Domandba0c292020-12-02 15:34:13 -08001089
George Liu0fda0f12021-11-16 10:06:17 +08001090 // Not expected that there will be multiple matching
1091 // CPU objects, but if there are just use the first
1092 // one.
Jonathan Domandba0c292020-12-02 15:34:13 -08001093
George Liu0fda0f12021-11-16 10:06:17 +08001094 // Use the common search routine to construct the
1095 // Collection of all Config objects under this CPU.
1096 collection_util::getCollectionMembers(
1097 asyncResp,
1098 "/redfish/v1/Systems/system/Processors/" + cpuName +
1099 "/OperatingConfigs",
1100 {"xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"},
1101 object.c_str());
1102 return;
1103 }
1104 },
1105 "xyz.openbmc_project.ObjectMapper",
1106 "/xyz/openbmc_project/object_mapper",
1107 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
1108 "/xyz/openbmc_project/inventory", 0,
1109 std::array<const char*, 1>{
1110 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"});
1111 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001112}
1113
1114inline void requestRoutesOperatingConfig(App& app)
1115{
1116 BMCWEB_ROUTE(
1117 app,
1118 "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001119 .privileges(redfish::privileges::getOperatingConfig)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001120 .methods(
1121 boost::beast::http::verb::get)([](const crow::Request& req,
1122 const std::shared_ptr<
1123 bmcweb::AsyncResp>& asyncResp,
1124 const std::string& cpuName,
1125 const std::string& configName) {
1126 // Ask for all objects implementing OperatingConfig so we can search
1127 // for one with a matching name
1128 crow::connections::systemBus->async_method_call(
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -06001129 [asyncResp, cpuName, configName, reqUrl{req.url}](
1130 boost::system::error_code ec,
1131 const dbus::utility::MapperGetSubTreeResponse& subtree) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001132 if (ec)
Jonathan Domandba0c292020-12-02 15:34:13 -08001133 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001134 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1135 << ec.message();
1136 messages::internalError(asyncResp->res);
1137 return;
Jonathan Domandba0c292020-12-02 15:34:13 -08001138 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001139 const std::string expectedEnding =
1140 cpuName + '/' + configName;
1141 for (const auto& [objectPath, serviceMap] : subtree)
Jonathan Domandba0c292020-12-02 15:34:13 -08001142 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001143 // Ignore any configs without matching cpuX/configY
1144 if (!boost::ends_with(objectPath, expectedEnding) ||
1145 serviceMap.empty())
1146 {
1147 continue;
1148 }
1149
1150 nlohmann::json& json = asyncResp->res.jsonValue;
1151 json["@odata.type"] =
1152 "#OperatingConfig.v1_0_0.OperatingConfig";
1153 json["@odata.id"] = reqUrl;
1154 json["Name"] = "Processor Profile";
1155 json["Id"] = configName;
1156
1157 // Just use the first implementation of the object - not
1158 // expected that there would be multiple matching
1159 // services
1160 getOperatingConfigData(
1161 asyncResp, serviceMap.begin()->first, objectPath);
1162 return;
Jonathan Domandba0c292020-12-02 15:34:13 -08001163 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001164 messages::resourceNotFound(asyncResp->res,
1165 "OperatingConfig", configName);
1166 },
1167 "xyz.openbmc_project.ObjectMapper",
1168 "/xyz/openbmc_project/object_mapper",
1169 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1170 "/xyz/openbmc_project/inventory", 0,
1171 std::array<const char*, 1>{
1172 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"});
1173 });
1174}
Jonathan Domandba0c292020-12-02 15:34:13 -08001175
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001176inline void requestRoutesProcessorCollection(App& app)
Gunnar Millsac6a4442020-10-14 14:55:29 -05001177{
Gunnar Millsac6a4442020-10-14 14:55:29 -05001178 /**
1179 * Functions triggers appropriate requests on DBus
1180 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001181 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/")
Ed Tanoused398212021-06-09 17:05:54 -07001182 .privileges(redfish::privileges::getProcessorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001183 .methods(boost::beast::http::verb::get)(
1184 [](const crow::Request&,
1185 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1186 asyncResp->res.jsonValue["@odata.type"] =
1187 "#ProcessorCollection.ProcessorCollection";
1188 asyncResp->res.jsonValue["Name"] = "Processor Collection";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001189
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001190 asyncResp->res.jsonValue["@odata.id"] =
1191 "/redfish/v1/Systems/system/Processors";
Gunnar Millsac6a4442020-10-14 14:55:29 -05001192
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001193 collection_util::getCollectionMembers(
1194 asyncResp, "/redfish/v1/Systems/system/Processors",
1195 std::vector<const char*>(processorInterfaces.begin(),
1196 processorInterfaces.end()));
1197 });
1198}
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)(
1209 [](const crow::Request&,
1210 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1211 const std::string& processorId) {
1212 asyncResp->res.jsonValue["@odata.type"] =
1213 "#Processor.v1_11_0.Processor";
1214 asyncResp->res.jsonValue["@odata.id"] =
1215 "/redfish/v1/Systems/system/Processors/" + processorId;
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001216
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001217 getProcessorObject(asyncResp, processorId, getProcessorData);
1218 });
Jonathan Doman3cde86f2020-12-02 14:50:45 -08001219
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001220 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001221 .privileges(redfish::privileges::patchProcessor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001222 .methods(boost::beast::http::verb::patch)(
1223 [](const crow::Request& req,
1224 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1225 const std::string& processorId) {
1226 std::optional<nlohmann::json> appliedConfigJson;
Willy Tu15ed6782021-12-14 11:03:16 -08001227 if (!json_util::readJsonPatch(req, asyncResp->res,
1228 "AppliedOperatingConfig",
1229 appliedConfigJson))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001230 {
1231 return;
1232 }
1233
1234 std::string appliedConfigUri;
1235 if (appliedConfigJson)
1236 {
1237 if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
1238 "@odata.id", appliedConfigUri))
1239 {
1240 return;
1241 }
1242 // Check for 404 and find matching D-Bus object, then run
1243 // property patch handlers if that all succeeds.
1244 getProcessorObject(
1245 asyncResp, processorId,
1246 [appliedConfigUri = std::move(appliedConfigUri)](
1247 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1248 const std::string& processorId,
1249 const std::string& objectPath,
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -06001250 const dbus::utility::MapperServiceMap& serviceMap) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001251 patchAppliedOperatingConfig(asyncResp, processorId,
1252 appliedConfigUri,
1253 objectPath, serviceMap);
1254 });
1255 }
1256 });
1257}
Gunnar Millsac6a4442020-10-14 14:55:29 -05001258
1259} // namespace redfish