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*d7857201SEd Tanous #include "dbus_singleton.hpp"
83ccb3adbSEd Tanous #include "dbus_utility.hpp"
93ccb3adbSEd Tanous #include "error_messages.hpp"
10*d7857201SEd Tanous #include "logging.hpp"
113ccb3adbSEd Tanous
12*d7857201SEd Tanous #include <boost/system/errc.hpp>
13e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
14*d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
15cf7eba09SNan Zhou
16*d7857201SEd Tanous #include <algorithm>
17e99073f5SGeorge Liu #include <array>
18cf7eba09SNan Zhou #include <charconv>
19*d7857201SEd Tanous #include <cstddef>
20*d7857201SEd Tanous #include <cstdint>
21*d7857201SEd Tanous #include <memory>
223544d2a7SEd Tanous #include <ranges>
23*d7857201SEd Tanous #include <span>
24*d7857201SEd Tanous #include <string>
25e99073f5SGeorge Liu #include <string_view>
26*d7857201SEd Tanous #include <system_error>
27*d7857201SEd Tanous #include <tuple>
28*d7857201SEd Tanous #include <utility>
29*d7857201SEd Tanous #include <vector>
30cf7eba09SNan Zhou
31c5d03ff4SJennifer Lee namespace redfish
32c5d03ff4SJennifer Lee {
33c5d03ff4SJennifer Lee
34b4bec66bSAbhishek Patel enum NetworkProtocolUnitStructFields
35b4bec66bSAbhishek Patel {
36b4bec66bSAbhishek Patel NET_PROTO_UNIT_NAME,
37b4bec66bSAbhishek Patel NET_PROTO_UNIT_DESC,
38b4bec66bSAbhishek Patel NET_PROTO_UNIT_LOAD_STATE,
39b4bec66bSAbhishek Patel NET_PROTO_UNIT_ACTIVE_STATE,
40b4bec66bSAbhishek Patel NET_PROTO_UNIT_SUB_STATE,
41b4bec66bSAbhishek Patel NET_PROTO_UNIT_DEVICE,
42b4bec66bSAbhishek Patel NET_PROTO_UNIT_OBJ_PATH,
43b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_0,
44b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_EMPTY,
45b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_ROOT_PATH
46b4bec66bSAbhishek Patel };
47b4bec66bSAbhishek Patel
48b4bec66bSAbhishek Patel enum NetworkProtocolListenResponseElements
49b4bec66bSAbhishek Patel {
50b4bec66bSAbhishek Patel NET_PROTO_LISTEN_TYPE,
51b4bec66bSAbhishek Patel NET_PROTO_LISTEN_STREAM
52b4bec66bSAbhishek Patel };
53b4bec66bSAbhishek Patel
54b4bec66bSAbhishek Patel /**
55b4bec66bSAbhishek Patel * @brief D-Bus Unit structure returned in array from ListUnits Method
56b4bec66bSAbhishek Patel */
57b4bec66bSAbhishek Patel using UnitStruct =
58b4bec66bSAbhishek Patel std::tuple<std::string, std::string, std::string, std::string, std::string,
59b4bec66bSAbhishek Patel std::string, sdbusplus::message::object_path, uint32_t,
60b4bec66bSAbhishek Patel std::string, sdbusplus::message::object_path>;
61b4bec66bSAbhishek Patel
62c5d03ff4SJennifer Lee template <typename CallbackFunc>
getMainChassisId(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,CallbackFunc && callback)63daadfb2eSEd Tanous void getMainChassisId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
64c5d03ff4SJennifer Lee CallbackFunc&& callback)
65c5d03ff4SJennifer Lee {
66c5d03ff4SJennifer Lee // Find managed chassis
67e99073f5SGeorge Liu constexpr std::array<std::string_view, 2> interfaces = {
68e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.Board",
69e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.Chassis"};
70e99073f5SGeorge Liu dbus::utility::getSubTree(
71e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces,
728cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback),
73e99073f5SGeorge Liu asyncResp](const boost::system::error_code& ec,
74b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) {
75c5d03ff4SJennifer Lee if (ec)
76c5d03ff4SJennifer Lee {
7762598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec);
78c5d03ff4SJennifer Lee return;
79c5d03ff4SJennifer Lee }
8026f6976fSEd Tanous if (subtree.empty())
81c5d03ff4SJennifer Lee {
8262598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find chassis!");
83c5d03ff4SJennifer Lee return;
84c5d03ff4SJennifer Lee }
85c5d03ff4SJennifer Lee
86f23b7296SEd Tanous std::size_t idPos = subtree[0].first.rfind('/');
87c5d03ff4SJennifer Lee if (idPos == std::string::npos ||
88c5d03ff4SJennifer Lee (idPos + 1) >= subtree[0].first.size())
89c5d03ff4SJennifer Lee {
90c5d03ff4SJennifer Lee messages::internalError(asyncResp->res);
9162598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't parse chassis ID!");
92c5d03ff4SJennifer Lee return;
93c5d03ff4SJennifer Lee }
94c5d03ff4SJennifer Lee std::string chassisId = subtree[0].first.substr(idPos + 1);
9562598e31SEd Tanous BMCWEB_LOG_DEBUG("chassisId = {}", chassisId);
96c5d03ff4SJennifer Lee callback(chassisId, asyncResp);
97e99073f5SGeorge Liu });
98c5d03ff4SJennifer Lee }
99b4bec66bSAbhishek Patel
100b4bec66bSAbhishek Patel template <typename CallbackFunc>
getPortStatusAndPath(std::span<const std::pair<std::string_view,std::string_view>> protocolToDBus,CallbackFunc && callback)1015c3e9272SAbhishek Patel void getPortStatusAndPath(
1025c3e9272SAbhishek Patel std::span<const std::pair<std::string_view, std::string_view>>
1035c3e9272SAbhishek Patel protocolToDBus,
104b4bec66bSAbhishek Patel CallbackFunc&& callback)
105b4bec66bSAbhishek Patel {
106b4bec66bSAbhishek Patel crow::connections::systemBus->async_method_call(
1078cb2c024SEd Tanous [protocolToDBus, callback = std::forward<CallbackFunc>(callback)](
1085e7e2dc5SEd Tanous const boost::system::error_code& ec,
109b4bec66bSAbhishek Patel const std::vector<UnitStruct>& r) {
1105c3e9272SAbhishek Patel std::vector<std::tuple<std::string, std::string, bool>> socketData;
111b4bec66bSAbhishek Patel if (ec)
112b4bec66bSAbhishek Patel {
11362598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec);
114b4bec66bSAbhishek Patel // return error code
1155c3e9272SAbhishek Patel callback(ec, socketData);
116b4bec66bSAbhishek Patel return;
117b4bec66bSAbhishek Patel }
118b4bec66bSAbhishek Patel
1195c3e9272SAbhishek Patel // save all service output into vector
120b4bec66bSAbhishek Patel for (const UnitStruct& unit : r)
121b4bec66bSAbhishek Patel {
122b4bec66bSAbhishek Patel // Only traverse through <xyz>.socket units
123bd79bce8SPatrick Williams const std::string& unitName =
124bd79bce8SPatrick Williams std::get<NET_PROTO_UNIT_NAME>(unit);
125b4bec66bSAbhishek Patel
126b4bec66bSAbhishek Patel // find "." into unitsName
127b4bec66bSAbhishek Patel size_t lastCharPos = unitName.rfind('.');
128b4bec66bSAbhishek Patel if (lastCharPos == std::string::npos)
129b4bec66bSAbhishek Patel {
130b4bec66bSAbhishek Patel continue;
131b4bec66bSAbhishek Patel }
132b4bec66bSAbhishek Patel
133b4bec66bSAbhishek Patel // is unitsName end with ".socket"
134b4bec66bSAbhishek Patel std::string unitNameEnd = unitName.substr(lastCharPos);
13555f79e6fSEd Tanous if (unitNameEnd != ".socket")
136b4bec66bSAbhishek Patel {
137b4bec66bSAbhishek Patel continue;
138b4bec66bSAbhishek Patel }
139b4bec66bSAbhishek Patel
140b4bec66bSAbhishek Patel // find "@" into unitsName
141b4bec66bSAbhishek Patel if (size_t atCharPos = unitName.rfind('@');
142b4bec66bSAbhishek Patel atCharPos != std::string::npos)
143b4bec66bSAbhishek Patel {
144b4bec66bSAbhishek Patel lastCharPos = atCharPos;
145b4bec66bSAbhishek Patel }
146b4bec66bSAbhishek Patel
147b4bec66bSAbhishek Patel // unitsName without "@eth(x).socket", only <xyz>
148b4bec66bSAbhishek Patel // unitsName without ".socket", only <xyz>
149b4bec66bSAbhishek Patel std::string unitNameStr = unitName.substr(0, lastCharPos);
150b4bec66bSAbhishek Patel
1515c3e9272SAbhishek Patel for (const auto& kv : protocolToDBus)
1525c3e9272SAbhishek Patel {
153b4bec66bSAbhishek Patel // We are interested in services, which starts with
154b4bec66bSAbhishek Patel // mapped service name
1555c3e9272SAbhishek Patel if (unitNameStr != kv.second)
156b4bec66bSAbhishek Patel {
157b4bec66bSAbhishek Patel continue;
158b4bec66bSAbhishek Patel }
159b4bec66bSAbhishek Patel
160b4bec66bSAbhishek Patel const std::string& socketPath =
161b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
162b4bec66bSAbhishek Patel const std::string& unitState =
163b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
164b4bec66bSAbhishek Patel
16589492a15SPatrick Williams bool isProtocolEnabled = ((unitState == "running") ||
16689492a15SPatrick Williams (unitState == "listening"));
1675c3e9272SAbhishek Patel
168e6feae51SAndrew Geissler // Some protocols may have multiple services associated with
169bd79bce8SPatrick Williams // them (for example IPMI). Look to see if we've already
170bd79bce8SPatrick Williams // added an entry for the current protocol.
1713544d2a7SEd Tanous auto find = std::ranges::find_if(
1723544d2a7SEd Tanous socketData,
173bd79bce8SPatrick Williams [&kv](const std::tuple<std::string, std::string, bool>&
174bd79bce8SPatrick Williams i) { return std::get<1>(i) == kv.first; });
175e6feae51SAndrew Geissler if (find != socketData.end())
176e6feae51SAndrew Geissler {
177bd79bce8SPatrick Williams // It only takes one enabled systemd service to consider
178bd79bce8SPatrick Williams // a protocol enabled so if the current entry already
179bd79bce8SPatrick Williams // has it enabled (or the new one is disabled) then just
180bd79bce8SPatrick Williams // continue, otherwise remove the current one and add
181bd79bce8SPatrick Williams // this new one.
182e6feae51SAndrew Geissler if (std::get<2>(*find) || !isProtocolEnabled)
183e6feae51SAndrew Geissler {
184bd79bce8SPatrick Williams // Already registered as enabled or current one is
185bd79bce8SPatrick Williams // not enabled, nothing to do
18662598e31SEd Tanous BMCWEB_LOG_DEBUG(
18762598e31SEd Tanous "protocolName: {}, already true or current one is false: {}",
18862598e31SEd Tanous kv.first, isProtocolEnabled);
189e6feae51SAndrew Geissler break;
190e6feae51SAndrew Geissler }
191bd79bce8SPatrick Williams // Remove existing entry and replace with new one
192bd79bce8SPatrick Williams // (below)
193e6feae51SAndrew Geissler socketData.erase(find);
194e6feae51SAndrew Geissler }
195e6feae51SAndrew Geissler
1965c3e9272SAbhishek Patel socketData.emplace_back(socketPath, std::string(kv.first),
1975c3e9272SAbhishek Patel isProtocolEnabled);
198b4bec66bSAbhishek Patel // We found service, return from inner loop.
1995c3e9272SAbhishek Patel break;
2005c3e9272SAbhishek Patel }
201b4bec66bSAbhishek Patel }
202b4bec66bSAbhishek Patel
2035c3e9272SAbhishek Patel callback(ec, socketData);
204b4bec66bSAbhishek Patel },
205b4bec66bSAbhishek Patel "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
206b4bec66bSAbhishek Patel "org.freedesktop.systemd1.Manager", "ListUnits");
207b4bec66bSAbhishek Patel }
208b4bec66bSAbhishek Patel
209b4bec66bSAbhishek Patel template <typename CallbackFunc>
getPortNumber(const std::string & socketPath,CallbackFunc && callback)210b4bec66bSAbhishek Patel void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
211b4bec66bSAbhishek Patel {
212deae6a78SEd Tanous dbus::utility::getProperty<
2131e1e598dSJonathan Doman std::vector<std::tuple<std::string, std::string>>>(
2141e1e598dSJonathan Doman *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath,
2151e1e598dSJonathan Doman "org.freedesktop.systemd1.Socket", "Listen",
2168cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback)](
2175e7e2dc5SEd Tanous const boost::system::error_code& ec,
2181e1e598dSJonathan Doman const std::vector<std::tuple<std::string, std::string>>& resp) {
219b4bec66bSAbhishek Patel if (ec)
220b4bec66bSAbhishek Patel {
22162598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec);
222b4bec66bSAbhishek Patel callback(ec, 0);
223b4bec66bSAbhishek Patel return;
224b4bec66bSAbhishek Patel }
22526f6976fSEd Tanous if (resp.empty())
226b4bec66bSAbhishek Patel {
227b4bec66bSAbhishek Patel // Network Protocol Listen Response Elements is empty
228b4bec66bSAbhishek Patel boost::system::error_code ec1 =
229b4bec66bSAbhishek Patel boost::system::errc::make_error_code(
230b4bec66bSAbhishek Patel boost::system::errc::bad_message);
231b4bec66bSAbhishek Patel // return error code
232b4bec66bSAbhishek Patel callback(ec1, 0);
23362598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec1);
234b4bec66bSAbhishek Patel return;
235b4bec66bSAbhishek Patel }
236b4bec66bSAbhishek Patel const std::string& listenStream =
2371e1e598dSJonathan Doman std::get<NET_PROTO_LISTEN_STREAM>(resp[0]);
238b4bec66bSAbhishek Patel const char* pa = &listenStream[listenStream.rfind(':') + 1];
239b4bec66bSAbhishek Patel int port{0};
240b4bec66bSAbhishek Patel if (auto [p, ec2] = std::from_chars(pa, nullptr, port);
241b4bec66bSAbhishek Patel ec2 != std::errc())
242b4bec66bSAbhishek Patel {
243b4bec66bSAbhishek Patel // there is only two possibility invalid_argument and
244b4bec66bSAbhishek Patel // result_out_of_range
245b4bec66bSAbhishek Patel boost::system::error_code ec3 =
246b4bec66bSAbhishek Patel boost::system::errc::make_error_code(
247b4bec66bSAbhishek Patel boost::system::errc::invalid_argument);
248b4bec66bSAbhishek Patel if (ec2 == std::errc::result_out_of_range)
249b4bec66bSAbhishek Patel {
250b4bec66bSAbhishek Patel ec3 = boost::system::errc::make_error_code(
251b4bec66bSAbhishek Patel boost::system::errc::result_out_of_range);
252b4bec66bSAbhishek Patel }
253b4bec66bSAbhishek Patel // return error code
254b4bec66bSAbhishek Patel callback(ec3, 0);
25562598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec3);
256b4bec66bSAbhishek Patel }
257b4bec66bSAbhishek Patel callback(ec, port);
2581e1e598dSJonathan Doman });
259b4bec66bSAbhishek Patel }
260b4bec66bSAbhishek Patel
261c5d03ff4SJennifer Lee } // namespace redfish
262