xref: /openbmc/bmcweb/features/redfish/lib/redfish_util.hpp (revision 177612aaa0633cf9d5aef0b763a43135cf552d9b)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
4c5d03ff4SJennifer Lee #pragma once
5c5d03ff4SJennifer Lee 
63ccb3adbSEd Tanous #include "async_resp.hpp"
7*177612aaSEd Tanous #include "boost_formatters.hpp"
8d7857201SEd Tanous #include "dbus_singleton.hpp"
93ccb3adbSEd Tanous #include "dbus_utility.hpp"
103ccb3adbSEd Tanous #include "error_messages.hpp"
11d7857201SEd Tanous #include "logging.hpp"
123f95a277SMyung Bae #include "utils/chassis_utils.hpp"
133ccb3adbSEd Tanous 
14d7857201SEd Tanous #include <boost/system/errc.hpp>
15e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
16d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
17cf7eba09SNan Zhou 
18d7857201SEd Tanous #include <algorithm>
19cf7eba09SNan Zhou #include <charconv>
20d7857201SEd Tanous #include <cstddef>
21d7857201SEd Tanous #include <cstdint>
22d7857201SEd Tanous #include <memory>
233544d2a7SEd Tanous #include <ranges>
24d7857201SEd Tanous #include <span>
25d7857201SEd Tanous #include <string>
26e99073f5SGeorge Liu #include <string_view>
27d7857201SEd Tanous #include <system_error>
28d7857201SEd Tanous #include <tuple>
29d7857201SEd Tanous #include <utility>
30d7857201SEd Tanous #include <vector>
31cf7eba09SNan Zhou 
32c5d03ff4SJennifer Lee namespace redfish
33c5d03ff4SJennifer Lee {
34c5d03ff4SJennifer Lee 
35b4bec66bSAbhishek Patel enum NetworkProtocolUnitStructFields
36b4bec66bSAbhishek Patel {
37b4bec66bSAbhishek Patel     NET_PROTO_UNIT_NAME,
38b4bec66bSAbhishek Patel     NET_PROTO_UNIT_DESC,
39b4bec66bSAbhishek Patel     NET_PROTO_UNIT_LOAD_STATE,
40b4bec66bSAbhishek Patel     NET_PROTO_UNIT_ACTIVE_STATE,
41b4bec66bSAbhishek Patel     NET_PROTO_UNIT_SUB_STATE,
42b4bec66bSAbhishek Patel     NET_PROTO_UNIT_DEVICE,
43b4bec66bSAbhishek Patel     NET_PROTO_UNIT_OBJ_PATH,
44b4bec66bSAbhishek Patel     NET_PROTO_UNIT_ALWAYS_0,
45b4bec66bSAbhishek Patel     NET_PROTO_UNIT_ALWAYS_EMPTY,
46b4bec66bSAbhishek Patel     NET_PROTO_UNIT_ALWAYS_ROOT_PATH
47b4bec66bSAbhishek Patel };
48b4bec66bSAbhishek Patel 
49b4bec66bSAbhishek Patel enum NetworkProtocolListenResponseElements
50b4bec66bSAbhishek Patel {
51b4bec66bSAbhishek Patel     NET_PROTO_LISTEN_TYPE,
52b4bec66bSAbhishek Patel     NET_PROTO_LISTEN_STREAM
53b4bec66bSAbhishek Patel };
54b4bec66bSAbhishek Patel 
55b4bec66bSAbhishek Patel /**
56b4bec66bSAbhishek Patel  * @brief D-Bus Unit structure returned in array from ListUnits Method
57b4bec66bSAbhishek Patel  */
58b4bec66bSAbhishek Patel using UnitStruct =
59b4bec66bSAbhishek Patel     std::tuple<std::string, std::string, std::string, std::string, std::string,
60b4bec66bSAbhishek Patel                std::string, sdbusplus::message::object_path, uint32_t,
61b4bec66bSAbhishek Patel                std::string, sdbusplus::message::object_path>;
62b4bec66bSAbhishek Patel 
63c5d03ff4SJennifer Lee template <typename CallbackFunc>
64daadfb2eSEd Tanous void getMainChassisId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65c5d03ff4SJennifer Lee                       CallbackFunc&& callback)
66c5d03ff4SJennifer Lee {
67c5d03ff4SJennifer Lee     // Find managed chassis
68e99073f5SGeorge Liu     dbus::utility::getSubTree(
693f95a277SMyung Bae         "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
708cb2c024SEd Tanous         [callback = std::forward<CallbackFunc>(callback),
71e99073f5SGeorge Liu          asyncResp](const boost::system::error_code& ec,
72b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
73c5d03ff4SJennifer Lee             if (ec)
74c5d03ff4SJennifer Lee             {
7562598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
76c5d03ff4SJennifer Lee                 return;
77c5d03ff4SJennifer Lee             }
7826f6976fSEd Tanous             if (subtree.empty())
79c5d03ff4SJennifer Lee             {
8062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find chassis!");
81c5d03ff4SJennifer Lee                 return;
82c5d03ff4SJennifer Lee             }
83c5d03ff4SJennifer Lee 
84f23b7296SEd Tanous             std::size_t idPos = subtree[0].first.rfind('/');
85c5d03ff4SJennifer Lee             if (idPos == std::string::npos ||
86c5d03ff4SJennifer Lee                 (idPos + 1) >= subtree[0].first.size())
87c5d03ff4SJennifer Lee             {
88c5d03ff4SJennifer Lee                 messages::internalError(asyncResp->res);
8962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't parse chassis ID!");
90c5d03ff4SJennifer Lee                 return;
91c5d03ff4SJennifer Lee             }
92c5d03ff4SJennifer Lee             std::string chassisId = subtree[0].first.substr(idPos + 1);
9362598e31SEd Tanous             BMCWEB_LOG_DEBUG("chassisId = {}", chassisId);
94c5d03ff4SJennifer Lee             callback(chassisId, asyncResp);
95e99073f5SGeorge Liu         });
96c5d03ff4SJennifer Lee }
97b4bec66bSAbhishek Patel 
98b4bec66bSAbhishek Patel template <typename CallbackFunc>
995c3e9272SAbhishek Patel void getPortStatusAndPath(
1005c3e9272SAbhishek Patel     std::span<const std::pair<std::string_view, std::string_view>>
1015c3e9272SAbhishek Patel         protocolToDBus,
102b4bec66bSAbhishek Patel     CallbackFunc&& callback)
103b4bec66bSAbhishek Patel {
104*177612aaSEd Tanous     dbus::utility::async_method_call(
1058cb2c024SEd Tanous         [protocolToDBus, callback = std::forward<CallbackFunc>(callback)](
1065e7e2dc5SEd Tanous             const boost::system::error_code& ec,
107b4bec66bSAbhishek Patel             const std::vector<UnitStruct>& r) {
1085c3e9272SAbhishek Patel             std::vector<std::tuple<std::string, std::string, bool>> socketData;
109b4bec66bSAbhishek Patel             if (ec)
110b4bec66bSAbhishek Patel             {
11162598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
112b4bec66bSAbhishek Patel                 // return error code
1135c3e9272SAbhishek Patel                 callback(ec, socketData);
114b4bec66bSAbhishek Patel                 return;
115b4bec66bSAbhishek Patel             }
116b4bec66bSAbhishek Patel 
1175c3e9272SAbhishek Patel             // save all service output into vector
118b4bec66bSAbhishek Patel             for (const UnitStruct& unit : r)
119b4bec66bSAbhishek Patel             {
120b4bec66bSAbhishek Patel                 // Only traverse through <xyz>.socket units
121bd79bce8SPatrick Williams                 const std::string& unitName =
122bd79bce8SPatrick Williams                     std::get<NET_PROTO_UNIT_NAME>(unit);
123b4bec66bSAbhishek Patel 
124b4bec66bSAbhishek Patel                 // find "." into unitsName
125b4bec66bSAbhishek Patel                 size_t lastCharPos = unitName.rfind('.');
126b4bec66bSAbhishek Patel                 if (lastCharPos == std::string::npos)
127b4bec66bSAbhishek Patel                 {
128b4bec66bSAbhishek Patel                     continue;
129b4bec66bSAbhishek Patel                 }
130b4bec66bSAbhishek Patel 
131b4bec66bSAbhishek Patel                 // is unitsName end with ".socket"
132b4bec66bSAbhishek Patel                 std::string unitNameEnd = unitName.substr(lastCharPos);
13355f79e6fSEd Tanous                 if (unitNameEnd != ".socket")
134b4bec66bSAbhishek Patel                 {
135b4bec66bSAbhishek Patel                     continue;
136b4bec66bSAbhishek Patel                 }
137b4bec66bSAbhishek Patel 
138b4bec66bSAbhishek Patel                 // find "@" into unitsName
139b4bec66bSAbhishek Patel                 if (size_t atCharPos = unitName.rfind('@');
140b4bec66bSAbhishek Patel                     atCharPos != std::string::npos)
141b4bec66bSAbhishek Patel                 {
142b4bec66bSAbhishek Patel                     lastCharPos = atCharPos;
143b4bec66bSAbhishek Patel                 }
144b4bec66bSAbhishek Patel 
145b4bec66bSAbhishek Patel                 // unitsName without "@eth(x).socket", only <xyz>
146b4bec66bSAbhishek Patel                 // unitsName without ".socket", only <xyz>
147b4bec66bSAbhishek Patel                 std::string unitNameStr = unitName.substr(0, lastCharPos);
148b4bec66bSAbhishek Patel 
1495c3e9272SAbhishek Patel                 for (const auto& kv : protocolToDBus)
1505c3e9272SAbhishek Patel                 {
151b4bec66bSAbhishek Patel                     // We are interested in services, which starts with
152b4bec66bSAbhishek Patel                     // mapped service name
1535c3e9272SAbhishek Patel                     if (unitNameStr != kv.second)
154b4bec66bSAbhishek Patel                     {
155b4bec66bSAbhishek Patel                         continue;
156b4bec66bSAbhishek Patel                     }
157b4bec66bSAbhishek Patel 
158b4bec66bSAbhishek Patel                     const std::string& socketPath =
159b4bec66bSAbhishek Patel                         std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
160b4bec66bSAbhishek Patel                     const std::string& unitState =
161b4bec66bSAbhishek Patel                         std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
162b4bec66bSAbhishek Patel 
16389492a15SPatrick Williams                     bool isProtocolEnabled = ((unitState == "running") ||
16489492a15SPatrick Williams                                               (unitState == "listening"));
1655c3e9272SAbhishek Patel 
166e6feae51SAndrew Geissler                     // Some protocols may have multiple services associated with
167bd79bce8SPatrick Williams                     // them (for example IPMI). Look to see if we've already
168bd79bce8SPatrick Williams                     // added an entry for the current protocol.
1693544d2a7SEd Tanous                     auto find = std::ranges::find_if(
1703544d2a7SEd Tanous                         socketData,
171bd79bce8SPatrick Williams                         [&kv](const std::tuple<std::string, std::string, bool>&
172bd79bce8SPatrick Williams                                   i) { return std::get<1>(i) == kv.first; });
173e6feae51SAndrew Geissler                     if (find != socketData.end())
174e6feae51SAndrew Geissler                     {
175bd79bce8SPatrick Williams                         // It only takes one enabled systemd service to consider
176bd79bce8SPatrick Williams                         // a protocol enabled so if the current entry already
177bd79bce8SPatrick Williams                         // has it enabled (or the new one is disabled) then just
178bd79bce8SPatrick Williams                         // continue, otherwise remove the current one and add
179bd79bce8SPatrick Williams                         // this new one.
180e6feae51SAndrew Geissler                         if (std::get<2>(*find) || !isProtocolEnabled)
181e6feae51SAndrew Geissler                         {
182bd79bce8SPatrick Williams                             // Already registered as enabled or current one is
183bd79bce8SPatrick Williams                             // not enabled, nothing to do
18462598e31SEd Tanous                             BMCWEB_LOG_DEBUG(
18562598e31SEd Tanous                                 "protocolName: {}, already true or current one is false: {}",
18662598e31SEd Tanous                                 kv.first, isProtocolEnabled);
187e6feae51SAndrew Geissler                             break;
188e6feae51SAndrew Geissler                         }
189bd79bce8SPatrick Williams                         // Remove existing entry and replace with new one
190bd79bce8SPatrick Williams                         // (below)
191e6feae51SAndrew Geissler                         socketData.erase(find);
192e6feae51SAndrew Geissler                     }
193e6feae51SAndrew Geissler 
1945c3e9272SAbhishek Patel                     socketData.emplace_back(socketPath, std::string(kv.first),
1955c3e9272SAbhishek Patel                                             isProtocolEnabled);
196b4bec66bSAbhishek Patel                     // We found service, return from inner loop.
1975c3e9272SAbhishek Patel                     break;
1985c3e9272SAbhishek Patel                 }
199b4bec66bSAbhishek Patel             }
200b4bec66bSAbhishek Patel 
2015c3e9272SAbhishek Patel             callback(ec, socketData);
202b4bec66bSAbhishek Patel         },
203b4bec66bSAbhishek Patel         "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
204b4bec66bSAbhishek Patel         "org.freedesktop.systemd1.Manager", "ListUnits");
205b4bec66bSAbhishek Patel }
206b4bec66bSAbhishek Patel 
207b4bec66bSAbhishek Patel template <typename CallbackFunc>
208b4bec66bSAbhishek Patel void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
209b4bec66bSAbhishek Patel {
210deae6a78SEd Tanous     dbus::utility::getProperty<
2111e1e598dSJonathan Doman         std::vector<std::tuple<std::string, std::string>>>(
2121e1e598dSJonathan Doman         *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath,
2131e1e598dSJonathan Doman         "org.freedesktop.systemd1.Socket", "Listen",
2148cb2c024SEd Tanous         [callback = std::forward<CallbackFunc>(callback)](
2155e7e2dc5SEd Tanous             const boost::system::error_code& ec,
2161e1e598dSJonathan Doman             const std::vector<std::tuple<std::string, std::string>>& resp) {
217b4bec66bSAbhishek Patel             if (ec)
218b4bec66bSAbhishek Patel             {
21962598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
220b4bec66bSAbhishek Patel                 callback(ec, 0);
221b4bec66bSAbhishek Patel                 return;
222b4bec66bSAbhishek Patel             }
22326f6976fSEd Tanous             if (resp.empty())
224b4bec66bSAbhishek Patel             {
225b4bec66bSAbhishek Patel                 // Network Protocol Listen Response Elements is empty
226b4bec66bSAbhishek Patel                 boost::system::error_code ec1 =
227b4bec66bSAbhishek Patel                     boost::system::errc::make_error_code(
228b4bec66bSAbhishek Patel                         boost::system::errc::bad_message);
229b4bec66bSAbhishek Patel                 // return error code
230b4bec66bSAbhishek Patel                 callback(ec1, 0);
23162598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec1);
232b4bec66bSAbhishek Patel                 return;
233b4bec66bSAbhishek Patel             }
234b4bec66bSAbhishek Patel             const std::string& listenStream =
2351e1e598dSJonathan Doman                 std::get<NET_PROTO_LISTEN_STREAM>(resp[0]);
236b4bec66bSAbhishek Patel             const char* pa = &listenStream[listenStream.rfind(':') + 1];
237b4bec66bSAbhishek Patel             int port{0};
238b4bec66bSAbhishek Patel             if (auto [p, ec2] = std::from_chars(pa, nullptr, port);
239b4bec66bSAbhishek Patel                 ec2 != std::errc())
240b4bec66bSAbhishek Patel             {
241b4bec66bSAbhishek Patel                 // there is only two possibility invalid_argument and
242b4bec66bSAbhishek Patel                 // result_out_of_range
243b4bec66bSAbhishek Patel                 boost::system::error_code ec3 =
244b4bec66bSAbhishek Patel                     boost::system::errc::make_error_code(
245b4bec66bSAbhishek Patel                         boost::system::errc::invalid_argument);
246b4bec66bSAbhishek Patel                 if (ec2 == std::errc::result_out_of_range)
247b4bec66bSAbhishek Patel                 {
248b4bec66bSAbhishek Patel                     ec3 = boost::system::errc::make_error_code(
249b4bec66bSAbhishek Patel                         boost::system::errc::result_out_of_range);
250b4bec66bSAbhishek Patel                 }
251b4bec66bSAbhishek Patel                 // return error code
252b4bec66bSAbhishek Patel                 callback(ec3, 0);
25362598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec3);
254b4bec66bSAbhishek Patel             }
255b4bec66bSAbhishek Patel             callback(ec, port);
2561e1e598dSJonathan Doman         });
257b4bec66bSAbhishek Patel }
258b4bec66bSAbhishek Patel 
259c5d03ff4SJennifer Lee } // namespace redfish
260