blob: 1f7c4badc064d1328020569abf3daf1b65afd3f1 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
Jennifer Leec5d03ff2019-03-08 15:42:58 -08004#pragma once
5
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08006#include "async_resp.hpp"
7#include "dbus_utility.hpp"
8#include "error_messages.hpp"
9
George Liue99073f2022-12-09 11:06:16 +080010#include <boost/system/error_code.hpp>
Nan Zhoucf7eba02022-07-21 23:53:20 +000011#include <sdbusplus/asio/property.hpp>
12
George Liue99073f2022-12-09 11:06:16 +080013#include <array>
Nan Zhoucf7eba02022-07-21 23:53:20 +000014#include <charconv>
Ed Tanous3544d2a2023-08-06 18:12:20 -070015#include <ranges>
George Liue99073f2022-12-09 11:06:16 +080016#include <string_view>
Nan Zhoucf7eba02022-07-21 23:53:20 +000017
Jennifer Leec5d03ff2019-03-08 15:42:58 -080018namespace redfish
19{
20
Abhishek Patelb4bec662021-06-21 17:32:02 -050021enum NetworkProtocolUnitStructFields
22{
23 NET_PROTO_UNIT_NAME,
24 NET_PROTO_UNIT_DESC,
25 NET_PROTO_UNIT_LOAD_STATE,
26 NET_PROTO_UNIT_ACTIVE_STATE,
27 NET_PROTO_UNIT_SUB_STATE,
28 NET_PROTO_UNIT_DEVICE,
29 NET_PROTO_UNIT_OBJ_PATH,
30 NET_PROTO_UNIT_ALWAYS_0,
31 NET_PROTO_UNIT_ALWAYS_EMPTY,
32 NET_PROTO_UNIT_ALWAYS_ROOT_PATH
33};
34
35enum NetworkProtocolListenResponseElements
36{
37 NET_PROTO_LISTEN_TYPE,
38 NET_PROTO_LISTEN_STREAM
39};
40
41/**
42 * @brief D-Bus Unit structure returned in array from ListUnits Method
43 */
44using UnitStruct =
45 std::tuple<std::string, std::string, std::string, std::string, std::string,
46 std::string, sdbusplus::message::object_path, uint32_t,
47 std::string, sdbusplus::message::object_path>;
48
Jennifer Leec5d03ff2019-03-08 15:42:58 -080049template <typename CallbackFunc>
Ed Tanousdaadfb22024-12-20 09:25:54 -080050void getMainChassisId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Jennifer Leec5d03ff2019-03-08 15:42:58 -080051 CallbackFunc&& callback)
52{
53 // Find managed chassis
George Liue99073f2022-12-09 11:06:16 +080054 constexpr std::array<std::string_view, 2> interfaces = {
55 "xyz.openbmc_project.Inventory.Item.Board",
56 "xyz.openbmc_project.Inventory.Item.Chassis"};
57 dbus::utility::getSubTree(
58 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -070059 [callback = std::forward<CallbackFunc>(callback),
George Liue99073f2022-12-09 11:06:16 +080060 asyncResp](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080061 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040062 if (ec)
63 {
64 BMCWEB_LOG_ERROR("{}", ec);
65 return;
66 }
67 if (subtree.empty())
68 {
69 BMCWEB_LOG_DEBUG("Can't find chassis!");
70 return;
71 }
Jennifer Leec5d03ff2019-03-08 15:42:58 -080072
Patrick Williamsbd79bce2024-08-16 15:22:20 -040073 std::size_t idPos = subtree[0].first.rfind('/');
74 if (idPos == std::string::npos ||
75 (idPos + 1) >= subtree[0].first.size())
76 {
77 messages::internalError(asyncResp->res);
78 BMCWEB_LOG_DEBUG("Can't parse chassis ID!");
79 return;
80 }
81 std::string chassisId = subtree[0].first.substr(idPos + 1);
82 BMCWEB_LOG_DEBUG("chassisId = {}", chassisId);
83 callback(chassisId, asyncResp);
84 });
Jennifer Leec5d03ff2019-03-08 15:42:58 -080085}
Abhishek Patelb4bec662021-06-21 17:32:02 -050086
87template <typename CallbackFunc>
Abhishek Patel5c3e9272021-06-24 10:11:33 -050088void getPortStatusAndPath(
89 std::span<const std::pair<std::string_view, std::string_view>>
90 protocolToDBus,
91 CallbackFunc&& callback)
Abhishek Patelb4bec662021-06-21 17:32:02 -050092{
93 crow::connections::systemBus->async_method_call(
Ed Tanous8cb2c022024-03-27 16:31:46 -070094 [protocolToDBus, callback = std::forward<CallbackFunc>(callback)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080095 const boost::system::error_code& ec,
Ed Tanousf94c4ec2022-01-06 12:44:41 -080096 const std::vector<UnitStruct>& r) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040097 std::vector<std::tuple<std::string, std::string, bool>> socketData;
98 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -070099 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400100 BMCWEB_LOG_ERROR("{}", ec);
101 // return error code
102 callback(ec, socketData);
103 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700104 }
105
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400106 // save all service output into vector
107 for (const UnitStruct& unit : r)
Ed Tanous002d39b2022-05-31 08:59:27 -0700108 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400109 // Only traverse through <xyz>.socket units
110 const std::string& unitName =
111 std::get<NET_PROTO_UNIT_NAME>(unit);
Ed Tanous002d39b2022-05-31 08:59:27 -0700112
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400113 // find "." into unitsName
114 size_t lastCharPos = unitName.rfind('.');
115 if (lastCharPos == std::string::npos)
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500116 {
117 continue;
118 }
119
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400120 // is unitsName end with ".socket"
121 std::string unitNameEnd = unitName.substr(lastCharPos);
122 if (unitNameEnd != ".socket")
Andrew Geisslere6feae52023-04-06 10:27:41 -0600123 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400124 continue;
Andrew Geisslere6feae52023-04-06 10:27:41 -0600125 }
126
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400127 // find "@" into unitsName
128 if (size_t atCharPos = unitName.rfind('@');
129 atCharPos != std::string::npos)
130 {
131 lastCharPos = atCharPos;
132 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700133
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400134 // unitsName without "@eth(x).socket", only <xyz>
135 // unitsName without ".socket", only <xyz>
136 std::string unitNameStr = unitName.substr(0, lastCharPos);
137
138 for (const auto& kv : protocolToDBus)
139 {
140 // We are interested in services, which starts with
141 // mapped service name
142 if (unitNameStr != kv.second)
143 {
144 continue;
145 }
146
147 const std::string& socketPath =
148 std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
149 const std::string& unitState =
150 std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
151
152 bool isProtocolEnabled = ((unitState == "running") ||
153 (unitState == "listening"));
154
155 // Some protocols may have multiple services associated with
156 // them (for example IPMI). Look to see if we've already
157 // added an entry for the current protocol.
158 auto find = std::ranges::find_if(
159 socketData,
160 [&kv](const std::tuple<std::string, std::string, bool>&
161 i) { return std::get<1>(i) == kv.first; });
162 if (find != socketData.end())
163 {
164 // It only takes one enabled systemd service to consider
165 // a protocol enabled so if the current entry already
166 // has it enabled (or the new one is disabled) then just
167 // continue, otherwise remove the current one and add
168 // this new one.
169 if (std::get<2>(*find) || !isProtocolEnabled)
170 {
171 // Already registered as enabled or current one is
172 // not enabled, nothing to do
173 BMCWEB_LOG_DEBUG(
174 "protocolName: {}, already true or current one is false: {}",
175 kv.first, isProtocolEnabled);
176 break;
177 }
178 // Remove existing entry and replace with new one
179 // (below)
180 socketData.erase(find);
181 }
182
183 socketData.emplace_back(socketPath, std::string(kv.first),
184 isProtocolEnabled);
185 // We found service, return from inner loop.
186 break;
187 }
188 }
189
190 callback(ec, socketData);
191 },
Abhishek Patelb4bec662021-06-21 17:32:02 -0500192 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
193 "org.freedesktop.systemd1.Manager", "ListUnits");
194}
195
196template <typename CallbackFunc>
197void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
198{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800199 dbus::utility::getProperty<
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700200 std::vector<std::tuple<std::string, std::string>>>(
201 *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath,
202 "org.freedesktop.systemd1.Socket", "Listen",
Ed Tanous8cb2c022024-03-27 16:31:46 -0700203 [callback = std::forward<CallbackFunc>(callback)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800204 const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700205 const std::vector<std::tuple<std::string, std::string>>& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400206 if (ec)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500207 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400208 BMCWEB_LOG_ERROR("{}", ec);
209 callback(ec, 0);
210 return;
Abhishek Patelb4bec662021-06-21 17:32:02 -0500211 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400212 if (resp.empty())
213 {
214 // Network Protocol Listen Response Elements is empty
215 boost::system::error_code ec1 =
216 boost::system::errc::make_error_code(
217 boost::system::errc::bad_message);
218 // return error code
219 callback(ec1, 0);
220 BMCWEB_LOG_ERROR("{}", ec1);
221 return;
222 }
223 const std::string& listenStream =
224 std::get<NET_PROTO_LISTEN_STREAM>(resp[0]);
225 const char* pa = &listenStream[listenStream.rfind(':') + 1];
226 int port{0};
227 if (auto [p, ec2] = std::from_chars(pa, nullptr, port);
228 ec2 != std::errc())
229 {
230 // there is only two possibility invalid_argument and
231 // result_out_of_range
232 boost::system::error_code ec3 =
233 boost::system::errc::make_error_code(
234 boost::system::errc::invalid_argument);
235 if (ec2 == std::errc::result_out_of_range)
236 {
237 ec3 = boost::system::errc::make_error_code(
238 boost::system::errc::result_out_of_range);
239 }
240 // return error code
241 callback(ec3, 0);
242 BMCWEB_LOG_ERROR("{}", ec3);
243 }
244 callback(ec, port);
245 });
Abhishek Patelb4bec662021-06-21 17:32:02 -0500246}
247
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800248} // namespace redfish